1

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

CrowHop
  • 49
  • 2
  • 7
  • Your example "Region (3, 19) has a mean value of 63 so that's a 1" doesn't match your code. 63 maps to 0. Please upload your input image, show us the output, and tell us why it's different from what you expect. – Cris Luengo Mar 20 '18 at 06:15
  • Well that's just a hypothetical example. I uploaded the "blackdot.jpg" image for reference. Also changed the example explanation to fit the logic. – CrowHop Mar 20 '18 at 18:46
  • append `\n` to the bitstream for each row processed, to align the output correctly – jackw11111 Mar 20 '18 at 23:11
  • That's what Im trying to do but it's all out of sorts. I updated my code to show you what I've got so far. – CrowHop Mar 21 '18 at 16:18

1 Answers1

0

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.

jackw11111
  • 1,457
  • 1
  • 17
  • 34