![]() |
| | #1 |
| Registered User Join Date: Aug 2006
Posts: 32
| Simple Image Processing 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;
}
}
}
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]; Code: data[i * step + j * 3] = 50; Code: if (data[i * step + j * 3] > 100)
{
// do something
}
Code: int x = data[i * step + j * 3]; std :: cout << x; It seems like it is something to do with converting between char and int...any ideas?? Thanks |
| ejohns85 is offline | |
| | #2 |
| Senior software engineer 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 | |
| | #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 | |
| | #4 |
| Senior software engineer 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 | |
| | #5 |
| Guest 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 ] 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |