Hello all

I have a problem at hand. I need to work on a C code which will read multiple images in RAW format (greyscale) and do an AND operation of corresponding pixels of all the images. Can someone help out on this? I am a novice and would definitely appreciate any help.

Thanks
J

So you have pixels represented as some number of shades of gray. And you want to AND them with something? Why AND? Is this some sort of mask where all ON (black? white?) is a mask to include the pixel and OFF is a mask to not include. In that case you could say:

for each row,
    for each column,
        result = pixel[row][col] & mask[row][col]; // '&' being bitwise-and

but if the mask is not all ON or all OFF bits (0x0 or 0xFF, say) then the resulting pixels will not be a gray scale but some odd shade. Like, if the mask is 0x5A and you mask that with a gray representing 20% gray you get a strange gray.

Its like i will be altering the image at variuos postions (on copies of the original image), and an AND operation is performed so that if it is a 1, then the particular pixel ahs not been altered. THe pixel will take values from 0 to 256 (8bits).


So you have pixels represented as some number of shades of gray. And you want to AND them with something? Why AND? Is this some sort of mask where all ON (black? white?) is a mask to include the pixel and OFF is a mask to not include. In that case you could say:

for each row,
    for each column,
        result = pixel[row][col] & mask[row][col]; // '&' being bitwise-and

but if the mask is not all ON or all OFF bits (0x0 or 0xFF, say) then the resulting pixels will not be a gray scale but some odd shade. Like, if the mask is 0x5A and you mask that with a gray representing 20% gray you get a strange gray.

You could just say....

if (mask_bit == 1)
result = original;
else
resilt = altered_pixel;

Becuase if the mask is one bit and the pixel is 8 bits you'd have to do some fussing with stuff to make AND work. If they are both 8 bits you could say:

result_pixel = mask_pixel & original_pixel;

but now the result is either 0 or original_pixel, you still have to supply the modified pixel.

Maybe some pseudocode or real code would help. What have you got so far?

OK .. i may have not put the problem thru clearly. I have a 512X512 pixel image. On which i make say 5 copies and make slight variations on these 5 images.What i have to do is read each pixel say at [1,1] of each of these 5 images and do an AND operation of all 5. Pls note that the original is not being involved, and no comparison is being made to the original. What is possible is that the alteration could be the same in a particular pixel? But that doesnt matter, if the value of a particular pixel is same in all the images then i neeed the location of the pixel. And operation is one way of finding out, if by ANdin the particular pixel of all the images is 1 then i need the pixel location.

Sorry if i am bothering u, but if you could get me a rough code then it would be great, im not so well versed in C.

Thank you
j

It's no bother at all, but you are right I don't understand what you are attempting. It sounds like you are doing something to these 5 copies and then seeing if doing something to them didn't change them. And it sounds like you are determined to use AND rather than ==, yet I don't get why.

Maybe it is time for someone else on this forum to chime in; I'm just being dense.

I want to use AND coz it is assumed that i dont have the original image. And by doing the AND operation i can know what part of the image(pixels) are the same in all of the images.

I hope this could clear your doubt.

u can use this code;

#include<stdio.h>
int main(void)
{
	FILE *fp;
	unsigned char image[256][256];
             int r,c;

if((fp = fopen("f16.dat","r+")) == NULL)
	{
		printf("Unable to open the specified file.\n");
		getch();
		exit(1);
	}
	else
	{
		for(r=0;r<256;r++)
		{
			for(c=0;c<256;c++)
			{
				image[r][c]=(unsigned char) fgetc (fp);
			}
		}
		fclose(fp);
}
getch();
return(0);
}

you can then alter the values in image array to play with the image.

I wrote a code but the output file is not being written (image file), but when reading the array i am getting the bits for each pixel. Can u take a look at it?

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>


#define size 512
#define num_images 2
using namespace std;


int main() {
char *filename[num_images] = {"92eb.raw","92eb.raw"};


unsigned char Imagedata[2];
unsigned char outputData;


FILE *input, *output;
for (int i = 0; i < num_images; i++) {
if (!(input = fopen(filename,"rb"))) {
cout << "Unable to open file" <<endl;
return 0;
}
fread(Imagedata,sizeof(unsigned char),size * size,input);
}


int result = 1;
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
for (int i = 0; i < num_images; i++) {
result=result & Imagedata[j][k];
}
outputData[j][k] = result;
result = 1;
}
}
if (!(output = fopen("output.raw", "wb"))) {
cout << "Could not open file for writing" << endl;
}


int ret = fwrite(outputData,sizeof(unsigned char), size * size,output);
cout << "Number of items written" << ret << endl;


return 1;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.