Iterate over regions and take mean pixel value in OpenCV?

时间秒杀一切 提交于 2021-01-28 11:50:38

问题


So Im basically trying to divide up a gray scale image (in this case 32x32) by resizing the initial image.

Once the "regions" are divided up, I need to take the mean pixel value of each one and then add to a string a 1, 0, or X. For example: "Region (3, 19) has a mean value of 21 so that's a 1".

I think I have most of the logic down but shouldn't, in theory, the output recreate the image in the form of 1s, 0s, and Xs? I feel like my math is wrong on the for loops maybe? Remember, all Im trying to do is break the image up into an MxN table or grid and taking the mean, 0 channel value of each grid region.

Here is my code:

Mat image = imread("blackdot.jpg", IMREAD_GRAYSCALE); //Pass in image
imshow("Gray scaled image", image);                     //imshow("Passed in gray scale image", image);

Mat resizedImage; // New Mat for saving the blown up image
resize(image, resizedImage, Size(3200, 3200)); // Blow up image so it's divisible by 32 using passed in image

string bitStream; // Ternary bitstream 

for (int y = 0; y<resizedImage.cols - 32; y += 32) {
    for (int x = 0; x<resizedImage.rows - 32; x += 32) {
        // get the average for the whole 32x32 block
        Rect roi(x, y, 32, 32);
        Scalar mean, dev;
        meanStdDev(resizedImage(roi), mean, dev); // mean[0] is the mean of the first channel, gray scale value;

        if (mean[0] >= 0 && mean[0] <= 25) {
            if ((counter % 3200) == 2900) {
                bitStream += "1\n";
                counter = 0;
            }
            else {
                bitStream += "1";
        }
        else if (mean[0] >= 77 && mean[0] <= 153) {
            if ((counter % 3200) == 2900) {
                bitStream += "X\n";
                counter = 0;
            }
            else {
                bitStream += "X";
        }
        else {
            if ((counter % 3200) == 2900) {
                bitStream += "0\n";
                counter = 0;
            }
            else {
                bitStream += "0";
        }
    }
}

cout << bitStream;

blackdot.jpg


回答1:


The code and logic looks good, for each pixel distribution, add a corresponding character to the bitstream and repeat that for all pixels in the row and for every column in the image.

When adding characters to the bitstream, try appending \n to the bitstream when a newline is reached (ie. when a row has been completed), to account for, in the bitstream, the alignment of the image. This equates to this minor adjustment in your code:

for (int y = 0; y<resizedImage.cols - 32; y += 32) {
    for (int x = 0; x<resizedImage.rows - 32; x += 32) {
        // get the average for the whole 32x32 block
        Rect roi(x, y, 32, 32);
        Scalar mean, dev;
        meanStdDev(resizedImage(roi), mean, dev); // mean[0] is the mean of the first channel, gray scale value;

        if (mean[0] >= 0 && mean[0] <= 25) {
            bitStream += "1";
        }
        else if (mean[0] >= 77 && mean[0] <= 153) {
            bitStream += "X";
        }
        else {
            bitStream += "0";
        }
    }
    //after each row has been checked, add newline
    bitStream += '\n';
}

Note: The output window may need maximizing to see correct results.



来源:https://stackoverflow.com/questions/49375685/iterate-over-regions-and-take-mean-pixel-value-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!