I'm trying to construct QImages from data saved as 16-bit integers in a binary file. I'm able to load the data in fine, but my program fails when I use the QImage::loadFromData(QBytearray ba) function (returning false) as follows:
QBytearray frame;
QImage pic = QImage(256, 256, QImage::Format_RGB888);
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
// Access value of pixel at each location
datum = store[i][j];
for(int c = 0; c < 3; c++) {
// Calculate colour at given pixel
col = (255.0f * ((float)datum - (float)min) / ((float)(max - min)));
// Assign colour value to the pixel
frame[c+3*j+3*i*width] = ((unsigned char)col);
}
}
}
pic.loadFromData(frame);
I repurposed this from Java code I had previously written which worked perfectly as intended (from the exact same data):
BufferedImage image = = new BufferedImage(256, 256, BufferedImage.TYPE_3BYTE_BGR);
byte[] data = image.getRaster().getDataBuffer();
for (j=0; j<height; j++) {
for (i=0; i<width; i++) {
//Find value of the pixels at the location
datum=data[j][i];
for (c=0; c<3; c++) {
//Calculate the colour at the given pixel
col=(255.0f*((float)datum-(float)min)/((float)(max-min)));
//Assign the colour value to the pixel
data[c+3*i+3*j*width] = (byte)col;
}
}
}
Can anybody help me to see where I'm getting this wrong? I've been stumped for days and am all out of ideas.
Ok, assuming that you are in fact trying to set RGB values for individual pixels, after reading the QImage details, I see you can do it using the following:
value = qRgb(189, 149, 39); // 0xffbd9527
image.setPixel(1, 1, value);
So, something like:
QImage pic = QImage(256, 256, QImage::Format_RGB888);
QRgb value;
int r,b,g;
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
// Access value of pixel at each location
datum = store[i][j];
//I get really confused what is going on here... you don't seem to be actually using `c` for the calculation?
for(int c = 0; c < 3; c++) { //let's just pretend you set the ints r,b,g in here somewhere
// Calculate colour at given pixel
col = (255.0f * ((float)datum - (float)min) / ((float)(max - min)));
}
// Assign colour value to the pixel
value = qRgb(r, g, b);
pic.setPixel(i, j, value);
}
}
Related
Given an n × n binary matrix image, flip the image horizontally, then invert it, and return the resulting image.
class Solution {
public int[][] flipAndInvertImage(int[][] image) {
for (int i = 0; i < image.length; i++) {
for (int j = image[0].length - 1; j >= 0; j--) {
image[i][j] ^= 1;
System.out.printf("%d ", image[i][j]);
}
}
return image;
}
}
This is my approach but when I return the 2D array not getting the desired output but you can see the stdout is printing result. May I know where I am going wrong
You're just printing the array not modifying it, You can modify the array using code below
public int[][] flipAndInvertImage(int[][] image) {
int[][] newImage = new int[image.length][image.length];
int l = 0, m;
for (int[] ints : image) {
m = 0;
for (int j = image[0].length - 1; j >= 0; j--) {
newImage[l][m++] = ints[j] ^ 1;
}
l++;
}
return newImage;
}
I am trying to implement various image processing filters on a ".pgm" file using java. Below is the code for minimum filter:
void applyMinFilter() {
int [] array = new int[size*size];
int t = 0, i = 0, j = 0;
for(int c = 0; c<h-size+1; c++) {
for(int k = 0; k<w-size+1; k++) {
t = 0;
for(i = c; i<c+size; i++) {
for(j = k; j<k+size; j++) {
array[t++] = matrix[i][j];
}
}
//placing the minimum value in the centre of the considered grid
matrix[i/2][j/2] = minimum(array);
}
}
}
Note: Here, size = 5, w = h = 400
Using this method, I am getting an output where my desired image is in one corner of the photo. You can see the output image by clicking here. In my code, the c loop and k loop help us traverse the whole of the image while the i loop and j loop provide us with the small window that we need to apply the minimum filter. I have already converted the ".pgm" image into a matrix for manipulation.
I am pretty sure that the error is coming from the line just after the comment line. I am not being able to properly place the minimum value pixel at the right location. What should I do?
you should replace the indexing of matrix[i/2][j/2] with matrix[c+size/2][k+size/2] OR you can make code as below:
void applyMinFilter() {
int [] array = new int[size*size];
int t = 0, i = 0, j = 0;
// for size = 5 you can use h-2 or w-2
// to make it general replace with h-size/2 and w-size/2
for(int c = 2; c < h-2; c++) {
for(int k = 2; k < w-2; k++) {
t = 0;
for(i = c-2; i < c+2; i++) {
for(j = k-2; j < k+2; j++) {
array[t++] = matrix[i][j];
}
}
//placing the minimum value in the centre of the considered grid
matrix[c][k] = minimum(array);
}
}
}
I'm programming a mobile game, something like the Arkanoid game. I've started trying to draw a matrix made by rectangles, so I wrote a "for" which goes over the columns and other "for" within the first one which goes over the rows. When I execute the App, this just draw an only row. I don't know what's wrong. Help please!
private void dibujarLadrillos(Canvas canvas, Paint paint)
{
int width = 0;
int height = 0;
Rect[] ladrillos;
for(int j = 0; j<= 2; j++) {
ladrillos = new Rect[5];
for (int i = 0; i <= ladrillos.length - 1; i++) {
ladrillos[i] = new Rect(width, height, width + getWidth() / 5 - 10, height + getHeight()/10 );
width += ladrillos[i].width() + 10;
canvas.drawRect(ladrillos[i], paint);
}
height+= ladrillos[0].height() + 10;
}
}
For each "j" you repeat the "i" loop.
So all but the last of the new Rect[5] you generate and fill are unreachable for you. The last one can be accessed via "ladrillos".
You need a 2-dimensional array like this:
Rect[][] ladrillos = new Rect[5][5];
The for loop has to be changed accordingly (I'm sure this needs some fine tuning but unfortunately I'm not familiar with Arkanoid ;-) )
for(int j = 0; j<= 2; j++) {
for (int i = 0; i <= ladrillos[j].length - 1; i++) {
ladrillos[j][i] = ...
width += ladrillos[j][i].width() + 10;
canvas.drawRect(ladrillos[j][i], paint);
}
height+= ladrillos[j][0].height() + 10;
}
I have problem how to start with this program.
I want to get 2d-array of pixel localization.
Then work on this array with bfs, dfs to get path from orange dot to green dot.
Draw grey pixel if visited.
Draw the path and save it to other image.
When i will handle with this i would like to change cost on each pixel (by drawing in paint something similar to walls but it could go throught by them with higher cost)
public int [][] gRGB(BufferedImage image)
{
int width = image.getWidth();
int height = image.getHeight();
int[][] result = new int[width][height];
for (int row = 0; row < width; row++) {
for (int col = 0; col < height; col++) {
result[row][col] = image.getRGB(row, col);
}
}
return result;
}
}
From this code i get 2d-array full of -1 value. Is there option to get color information as value (not rgb i would like to have it as one number not 4)
EDIT:
protected static int [][] convert(BufferedImage image)
{
int width = image.getWidth();
int height = image.getHeight();
int[][] result = new int [width][height];
for(int row = 0; row < height; row++)
for(int col = 0; col < width; col++)
{
Color c = new Color(image.getRGB(col, row));
String h = String.format("%02x%02x%02x", c.getRed(),c.getGreen(),c.getBlue());
if(h.equals("000000")) // black
{
result[col][row] = 0;
}
else if(h.equals("fe0000")) // red
{
result[col][row] = 5;
}
else if(h.equals("ffffff")) // white
{
result[col][row] = 1;
}
else if(h.equals("ff7d41")) // orange - start
{
result[col][row] = 10;
}
else if (h.equals("ff0078")) // pink - end
{
result[col][row] = 9;
}
else
{
result[col][row] = 3;
}
}
for(int row = 0; row < height; row++)
{
System.out.println();
for(int col = 0; col < width; col++)
System.out.print("\t" + result[col][row]);
}
return result;
}
So i have now the array of pixel value. Can someone explain me how to write DFS or BFS algorithm?? Where the cost is the value of pixel?
Black - walls, Orange dot - start, Green dot - end
For finding the path with minimum cost it is better to use algorithms such as UCS,A* or IDA* (It is very inefficient to use BFS or DFS to find shortest path on a weighted graph). My suggestion is to first implement UCS , then improve it with a simple heuristic such as manhattan distance to A*. For full explanation about UCS and A* please refer to these links:
Wikipedia A*
Wikipedia UCS
As for using these algorithms on your 2D-Array, you should consider every point a node and connect that node to every neighbor nodes. So every node is connected to its 4 non-wall neighbors ( or 8 non-wall neighbors if you can move diagonally ).
This is what I am after:
This is what I make:
The problem with mine is that it just differs in color very close to the black part, then its the same constant blue. Compared to the first one where it more gradually increases into a deeper blue..
this is my color code:
public Generator(){
array = new Color[200];
for(int k=0; k<200; k++){
array[k]= new Color(k,k*,255);
}
I made a 200 big color array, with a different color in each slot. Then I add color to every slot in my window with the following code:
for (int i = 0; i < Xheight; i++) {
for (int j = 0; j < Xwidth; j++) {
Complex c = new Complex(0,0);
Complex z = new Complex(0, 0);
c.add(matris[k+(resolution*i)-1][k+(resolution*j)-1]);
int d=0;
while(d<LordOfDoom && z.getAbs2()<4){
z.mul(z);
z.add(c);
d++;
}
if (z.getAbs2()<=4 ){
picture[i][j]=Color.black;
}
}
if (z.getAbs2()>4 &&color==1 ){
picture[i][j]=array[d-1];
}
}
}
Basically it gets a picture depending on how fast z.getAbs>4.