opencv,选择性着色效果

时间:2014-03-19 20:27:08

标签: opencv image-processing colors

我想对图像产生影响,使特定颜色保持不变,其余颜色变为灰度。但是,我的程序输出非常糟糕,有人可以帮我改进我的代码吗?当坐标超过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;
}

1 个答案:

答案 0 :(得分:1)

  • 放弃过时的c-api,它已经死了&amp;走了。
  • 您已经了解了inRange(),将结果用作进一步处理的掩码
  • 已经有cvtColor(src,dst,CV_BGR2GRAY),更喜欢内置的东西来自己滚动
  • opencv是一个计算机视觉/ ml库,你正在滥用它作为二级Photoshop。 - 不要!