Hey guys, I'm a Computer Engineering Senior trying to do some computer vision for my design project. I'm using the summer to get acquainted with computer vision, as I've never taken a course in it nor have I any experience with it at all. So first off, thank you to the admin for the great tutorials posted on the subject! They were very helpful on getting me started off in the right direction. On to my problem...:-)
I wrote some code up in OpenCV to try and filter out everything but red objects. Here are some relevant snippets of code, and the resulting images...
First I apply an RGB max algorithm, it doesn't actually "max" the dominant color of a pixel, it just zero's the non dominant colors:
for( int y=0; y<frame->height; y++ ) {
uchar* ptr = (uchar*) (
frame->imageData + y * frame->widthStep
);
for( int x=0; x<frame->width; x++ ) {
if ( ptr[RED] > ptr[GREEN] && ptr[RED] > ptr[BLUE] ) {
//ptr[RED] = 255;
ptr[BLUE] = 0;
ptr[GREEN] = 0;
}
if ( ptr[GREEN] > ptr[RED] && ptr[GREEN] > ptr[BLUE] ) {
ptr[RED] = 0;
ptr[BLUE] = 0;
//ptr[GREEN] = 255;
}
if ( ptr[BLUE] > ptr[GREEN] && ptr[BLUE] > ptr[RED] ) {
ptr[RED] = 0;
//ptr[BLUE] = 255;
ptr[GREEN] = 0;
}
}
}
Then I apply a red threshold filter, it filters both the hue and intensity of the pixels. It looks like this:
for( int y=0; y<frame->height; y++ ) {
uchar* ptr = (uchar*) (
frame->imageData + y * frame->widthStep
);
for( int x=0; x<frame->width; x++ ) {
double r(ptr[RED]),g(ptr[GREEN]),b(ptr[BLUE]);
double light(r*.2 + g*.7 + b*.1);
if (light >= lightThresh ) {
ptr[BLUE] = 0;
ptr[GREEN] = 0;
ptr[RED] = 0;
}
}
}
for( int y=0; y<frame->height; y++ ) {
uchar* ptr = (uchar*) (
frame->imageData + y * frame->widthStep
);
for( int x=0; x<frame->width; x++ ) {
if ( ptr[RED] >= redThresh) {
ptr[BLUE] = 0;
ptr[GREEN] = 0;
//ptr[RED] = 255;
}
else {
ptr[BLUE] = 0;
ptr[GREEN] = 0;
ptr[RED] = 0;
}
}
}
This results in poor red filtering, as can be seen below:
No filters:
RGB "Max":
RGB Max + Red Threshold
I've played around with the threshold value for about an hour, and these are the best results I can get. If the hue threshold is too low, my entire body is seen as red, even my shirt, if it's too high not even the paper is seen as red. The light threshold gives me similar headaches. I can only conclude that I'm doing something wrong, or at least less than optimal
.
Any tips or help to getting better results would be greatly appreciated, thanks!