彩色到灰度转换

问题描述 投票:0回答:1

Im使用C ++ openCV程序实现第一原理HDL(Verilog)图像对象检测的算法开发。我终于设法使HDL版本达到精明的检测点。为了验证两者,两者都需要具有相同的输出。我发现它们之间的细微差别是由openCV读取的颜色到灰度转换偏向绿色造成的。在openCV C ++方法中,平滑的图像总体上更亮。通过查看rgb2gray方法,似乎openCV使用了一个偏差,即(R X + G Y + B * Z)/ 3,而在HDL中,我一直在使用(R + G + B)/ 3完成高斯,Sobel和Canny滤波器。人类可视化是次要的,并且不以非整数相乘是不希望的。

是否有用于转换的标准线性灰度转换或替代现有方法的方法?...

int main()
{
            int thold = 15;

            clock_t start;
            double duration;
            const int sobelX[3][3] = { {-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1} };  //Where origionally floats in python
            const int sobelY[3][3] = { {-1, -2, -1}, {0, 0, 0}, {1, 2, 1} }; //Where origionally floats in python
            const int kernel[5][5] = { {1,6,12,6,1},
                                                                                                {6,42,79,42,6},
                                                                                                                        {12,79,148,79,12},
                                                                                                                        {6,42,79,42,6},
                                                                                                                        {1,6,12,6,1} };// 1/732
            // Above normalised kernal for smoothing,  see origional python script for method 
            start = std::clock();
            int height, width, intPixel, tSx, tSy, tS, dirE, dirEE, maxDir, curPoint, contDirection, cannyImgPix, nd, tl, tm, tr, mr, br, bm, bl, ml = 0;
            int contNum = 128;
            int contPixCount = 0;
            int curContNum = 0;
            int contPlace = 0;
            int oldContPlace = 0;
            int g = 0;
            bool maxPoint;
            struct pixel {
                        int number;
                        int h;
                        int w;

            };
            std::vector<pixel> contourList;
            //double floatPixel = 0.0;
            int kernalCumulator = 0;
            const int mp = 3;
        //  Scalar color(0, 0, 255);
            //          duration = ((clock()) - start) / (double)CLOCKS_PER_SEC;
            //          start = clock();
            //          cout << "Start image in" << duration << '\n';
            //          Mat dst;
            Mat rawImg = imread("C:\\Users\\&&&\\Documents\\pycode\\paddedGS.png",0);
            printf("%d",rawImg.type());

//          Mat rawImg = imread("C:\\Users\\&&&\\Documents\\openCV_Master\\openCVexample\\openCVexample\\brace200.jpg ", 0);
            height = rawImg.rows;
            width = rawImg.cols;
            cout << "Height of image " << height << '\n';
            cout << "Width of image " << width << '\n';
            Mat filteredImg = Mat::zeros(height, width, CV_8U);
            printf("%d", filteredImg.type());
            Mat sobelImg = Mat::zeros(height, width, CV_8U);
            Mat directionImg = Mat::zeros(height, width, CV_8U);
            Mat cannyImg = Mat::zeros(height, width, CV_8U);
            Mat contourImg = Mat::zeros(height, width, CV_16U);

//          rawImg.convertTo(rawImg, CV_8UC1);

            duration = ((clock()) - start) / (double)CLOCKS_PER_SEC;
            start = clock();
            cout << "Start image in" << duration << '\n';
            // Loop to threshold already grayscaled image           
            /*
            for (int h = 0; h < (height); h++)
            {
                        for (int w = 0; w < (width); w++)
                        {
                                    g = (int)rawImg.at<uchar>(h, w,0);
                                    cout << g << "g";
                                    g+= (int)rawImg.at<uchar>(h, w, 1);
                                    cout << g << "g";
                                    g+= (int)rawImg.at<uchar>(h, w, 2);
                                    cout << g << "g";
                                    g = g/3;
                                    rawGImg.at<uchar>(h,w) = g;
                        }
            }

            */
            //          imshow("thresholded Image", rawImg);
            //          waitKey();
            // Loop to smooth using Gausian 5 x 5 kernal

//          imshow("raw Image", rawImg);


            for (int h = 3; h < (height - 3); h++)
            {
                        for (int w = 3; w < (width - 3); w++)
                        {
                                    if (rawImg.at<uchar>(h, w) >=6 )//Thresholding included
                                    {
                                                for (int xk = 0; xk < 5; xk++)
                                                {
                                                            for (int yk = 0; yk < 5; yk++)
                                                            {
                                                                        intPixel = rawImg.at<uchar>((h + (xk - mp)), (w + (yk - mp)));
                                                                        kernalCumulator += intPixel*(kernel[xk][yk]);//Mutiplier required as rounding is making number go above 255,  better solution?
                                                            }
                                                }
                                    }
                                    else
                                                kernalCumulator = 0;

                                    kernalCumulator = kernalCumulator / 732;
                                    if (kernalCumulator < 0 || kernalCumulator > 255)
                                    {
        //                                      cout << "kernal Value: " << kernalCumulator;
            //                                  cout << " intPixel:" << intPixel << '\n';
                                    }
                                    filteredImg.at<uchar>(h, w) = (uchar)kernalCumulator;
                                    kernalCumulator = 0;
                        }
            }
c++ opencv grayscale hdl
1个回答
0
投票

我们的视觉不能线性地感知亮度,因此对于通常的应用而言,使用某种模仿人类感知的变换是有意义的。

对于您的应用程序,您有2个选择:要么在HDL中使用类似的转换(这可能不容易,要么不期望),或者为OpenCV使用自定义的rgb进行灰度转换,以使用您使用的相同转换。

这是一个简短的代码段(更像伪代码,您必须弄清楚细节),类似于:

cv::Mat linearRgbToGray(const cv::Mat &color) {
    cv::Mat gray(color.size(), CV_8UC1);
    for (int i = 0; i < color.rows; i++)
        for (int j = 0; j < color.cols; j++)
           gray.at(i, j) = (color.at(i, j)[0] + color.at(i, j)[1] + color.at(i, j)[2]) / 3;
}
© www.soinside.com 2019 - 2024. All rights reserved.