C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2009, 10:40 AM   #1
Registered User
 
Join Date: Aug 2006
Posts: 32
Simple Image Processing

Hello,

I am trying to do some very simple image processing, using OpenCV to access data from an image. What I want to do is check whether the "B" value of each RGB pixel is greater than 100 - and if so, set that "B" value to 255.

Here is my code:

Code:
IplImage* image = cvLoadImage("C:\\myPicture.bmp"); // Pointer to OpenCV IplImage structure
data = (char*)image -> imageData; // char buffer of image data
height = originalImage -> height; // height of image
width = originalImage -> width; // width of image
step = originalImage -> widthStep; // size in bytes of one line in image

for (int i = 0; i < height; i ++)
	{
		for (int j = 0; j < width; j ++)
		{
			if ((int)data[i * step + j * 3] > 100)
			{
				data[i * step + j * 3] = 255;
                                                 }
		}
	}
The problem is, the line which sets the "B" component to zero is never reached. In fact, using a breakpoint in debugging mode, I noticed that the value of data[i * step + j * 3] is always 0...

I have checked that the image is displaying correctly by displaying the image - so it is loading into the buffer ok.

I am able to do SOME image processing, such as inversion, where I simply say :

Code:
data[i * step + j * 3] = 255 - data[i * step + j * 3];
Also, if I assign a value to an element of the char array, that works too, such as making the entire image dark grey by saying :

Code:
data[i * step + j * 3] = 50;
BUT, when I actually try to compare the value in the char array to a number, such as in thresholding :

Code:
if (data[i * step + j * 3] > 100)
{
// do something
}
It doesn't work. I have tried checking the values of the array, for example :

Code:
int x = data[i * step + j * 3];
std :: cout << x;
...and it always tells me that the value of x is 0, no matter which element of the image buffer I am accessing.

It seems like it is something to do with converting between char and int...any ideas??

Thanks
ejohns85 is offline   Reply With Quote
Old 03-19-2009, 11:43 AM   #2
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
So, what's the difference between image and originalImage?

I see nothing immediately wrong.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 03-19-2009, 11:53 AM   #3
Registered User
 
Join Date: Aug 2006
Posts: 32
Ooops..."yeh originalImage" should just be "image". They are the same.

Can't work out why I can assign values to the data, but I cannot compare the values in an if statement.

If you say that it all looks fine, then I guess there is something going on at a deeper level I will have to look at...
ejohns85 is offline   Reply With Quote
Old 03-19-2009, 11:58 AM   #4
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Hmm. You may have a signedness issue.

Since the data pointer is char, any value greater than 127 will be treated as negative. When that casts to int, it's still negative, so your > 100 comparison will fail.

That still doesn't explain why everything in the array appears to be 0, but it's an issue. Try using an unsigned char * to point at the image data, instead of a char *.

Also, are you treating the pixel format correctly? Is this truly 24-bit RGB, or could there be a pad byte to make each pixel 32 bits wide?
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 03-19-2009, 12:10 PM   #5
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,034
Well, assuming that each rgb value is packed into 3 consecutive bytes, wouldn't your blue values be indexed by:

Code:
data[ i * step * 3 + j * 3 + 2 ]
Incidentally, you could make your life a lot easier if you just treated the buffer as an array of RGB's, instead of chars...

Code:
#pragma pack(push, 1)
struct rgb
{
	unsigned char red, green, blue;
};
#pragma pack(pop)


IplImage* image = cvLoadImage("C:\\myPicture.bmp"); // Pointer to OpenCV IplImage structure
rgb* data = (rgb*)image -> imageData; // char buffer of image data
height = originalImage -> height; // height of image
width = originalImage -> width; // width of image
step = originalImage -> widthStep; // size in bytes of one line in image

for (int i = 0; i < height; i ++)
{
	for (int j = 0; j < width; j ++)
	{
		if (data[i * step + j].blue > 100)
		{
			data[i * step + j].blue = 255;
		}
	}
}
Sebastiani is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Program Plan Programmer_P C++ Programming 0 05-11-2009 01:42 AM
Im getting an error and i cant figure out why. please help. ominub C Programming 8 04-11-2009 01:49 AM
Image rotation - doesn't always work ulillillia C Programming 12 05-03-2007 12:46 PM
C++ Image Processing Homework (dynamic 2d array binary i/o trouble) Xixchil C++ Programming 4 03-02-2003 09:12 AM
Tab Controls - API -KEN- Windows Programming 7 06-02-2002 09:44 AM


All times are GMT -6. The time now is 10:59 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22