我想对图像产生影响,使特定颜色保持不变,其余颜色变为灰度。但是,我的程序输出非常糟糕,有人可以帮我改进我的代码吗?当坐标超过500时,它会中断,并且显示的颜色范围太小。提前致谢
代码:
int i1=10, i2=50, a = 10;// coordinates of pixel value, whose color must remain; hue range
IplImage* GetNewImage(IplImage* frame, IplImage* imgHSV){
IplImage* imgNew = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
IplImage* imgBW = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
CvScalar s1;
CvScalar s2;
CvScalar s3;
s3 = cvGet2D(imgHSV, i1, i2);
cvInRangeS(imgHSV, cvScalar(s3.val[0] - 10, s3.val[1] - 50, s3.val[2] - 50), cvScalar(s3.val[0] + 10, s3.val[1] + 50, s3.val[2] + 50), imgBW);
imgNew = cvCloneImage(frame);
for (int i = 0; i < frame->height; i++) for (int j = 0; j < frame->width; j++) {
s1 = cvGet2D(imgBW, i, j);
s2 = cvGet2D(imgNew, i, j);
if (s1.val[0] == 0){
for (int k = 0; k < 3; k++){
s3.val[k] = (s2.val[0] * 0.114 + s2.val[1] * 0.587 + s2.val[2] * 0.299);//converting to grayscale
cvSet2D(imgNew, i, j, s3);
}
}
}
cvReleaseImage(&imgBW);
return imgNew;
}
答案 0 :(得分:1)