遍历区域并在OpenCV中获取平均像素值?

问题描述:

因此,Im基本上是尝试通过调整初始图像的大小来分割灰度图像(在这种情况下为32x32).

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

一旦区域"被分割,我需要取每个像素的平均像素值,然后将1、0或X添加到字符串中.例如:区域(3,19)的均值值21,所以是1.

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".

我认为我掌握了大部分逻辑,但从理论上讲,输出不应该以1s,0s和Xs的形式重新创建图像吗?我觉得for循环上的数学错误吗?请记住,我只是想将图像分解成MxN表格或网格,并取每个网格区域的平均值0通道值.

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.

这是我的代码:

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

代码和逻辑看起来不错,对于每个像素分布,在位流中添加一个相应的字符,并对行中的所有像素和其中的每一列重复该字符图片.

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.

在向位流中添加字符时,请尝试在到达换行符时(即,当一行已完成时)在位流中添加 \ n ,以在位流中说明对齐方式图片.这等同于您的代码中的微小调整:

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.