【发布时间】:2014-02-23 02:39:28
【问题描述】:
我必须区分 5 种类型的图像,这些图像可能主要是红色、绿色、蓝色、橙色或黄色,还有一些白色或黑色。我必须找到图像中突出的颜色。
图像的来源是网络摄像头,因此实际颜色还取决于图像与网络摄像头的照明和距离。可以在这里看到我的图像示例:
http://ian-albert.com/hazmat_placards/placard-2-flammable-gas.png
我正在尝试根据“色调”值计算百分比。我为每种颜色指定了一些范围。我的范围是:
红色: 0-10
绿色: 50-65
黄色: 18-21
蓝色: 100-115
问题:即使显示的图像不是红色,我的红色百分比仍然很高。
我的代码如下:
int findRect::checkByHSV(int svmResult, Mat detectedSquare)
{
Mat hsv_img;
cvtColor(detectedSquare,hsv_img,CV_BGR2HSV);
Vec3b pixel;
float totalPixel=0; // to count the total number of pixels in an image---to get the Percentage later
float totalClass[6];// because we want to test for 5 classes+ a garbage class.{{ Class-0 -> Garbage, Class-1->Orange, Class-2->Green, Class-3->Red,
// Class-4->Blue, Class-5->Yellow }}
for(int i=0; i<hsv_img.rows; i++)
{
for (int j=0; j<hsv_img.cols; j++)
{
totalPixel++;
pixel= hsv_img.at<Vec3b>(i,j);
if( pixel[0]>0 && pixel[0]<1 ) totalClass[1]++; // Class-1->Orange
else if ( pixel[0]>50 && pixel[0]<65 ) totalClass[2]++; // To check Green class-2 //svmResult==2 &&
else if ( pixel[0]>0 && pixel[0]<10 ) totalClass[3]++; // Class-3->Red
else if ( pixel[0]>100 && pixel[0]<115 ) totalClass[4]++; // Class-4->Blue
else if ( pixel[0]>18 && pixel[0]<21 ) totalClass[5]++; // Class-5->Yellow
}
}
float percentage[5];
totalClass[0]=0; //Putting zero to the Garbage class
for (int i=0; i<=5; i++)
{
percentage[i] = (totalClass[i] / totalPixel )*100;
}
cout<<"\n Organge: "<<percentage[1]<<" Green: "<<percentage[2]<<" Red: "<<percentage[3]<<" Blue: "<<percentage[4]<<" Yellow: "<<percentage[5]<<"\n \n";
return svmResult;
}
【问题讨论】:
-
图片是什么颜色的?此外,OpenCV 色相谱环绕。红色将是 0-10 和 170-180
-
@Osiris:正如我已经提到的,图像可以主要由红色、绿色、橙色、蓝色或黄色组成......还有一些黑色或白色。
-
不,我指的是这张特殊的图片。你是说显示的图像不是红色的。你期待什么输出?也许您可以在帖子中包含图片。
-
我无法在 SO 中嵌入图像。我的意思是任何不是红色的图像,即主要是绿色、蓝色或黄色。问题是,对于这些图像,我的红色百分比很高。
-
黄色图像出现重大问题...