Ask Your Question
-2

Decomposing an image to several subimages

asked 2017-12-05 12:10:14 -0600

Digital Design gravatar image

updated 2017-12-05 14:08:39 -0600

berak gravatar image

Hello, I am gonna divide a 48004800 RGB image (Host_Image) to 1616 RGB sub-images each consisting of 300*300 pixels. Sub_Image (0,0): Host_Image(0, 0), Host_Image(0, 16), Host_Image(0, 32), … Host_Image(16, 0), Host_Image(16,16), Host_Image(16, 32), … …

Host_Image(4784, 0), Host_Image(4784,16), Host_Image(4784, 32), …

Sub_Image (0,1): Host_Image(0, 1), Host_Image(0, 17), Host_Image(0, 33), … Host_Image(16, 1), Host_Image(16,17), Host_Image(16, 33), … …

Host_Image(4785, 0), Host_Image(4785,16), Host_Image(4785, 32), …

Sub_Image (1, 0): Host_Image(1, 0), Host_Image(17, 0), Host_Image(33, 0), … Host_Image(17, 0), Host_Image(17,16), Host_Image(17, 32), … … Host_Image(4785, 0), Host_Image(4785,16), Host_Image(4785, 32), …

It may seem a super-banal task. But when I save the sub-images, the saved values are totally different with the pixels of the host image. I obtain 16*16 error log which shows some pixels are mapped correctly while many others are nor. Any idea?

vector<Mat> res;
register unsigned int i1, j1, k, l;
//pixels_num= the number of pixels per uI
char str[200], cnt;
uchar n_ch = po.channels();
Sub_Image_Width = 300;
Sub_Image_Length = 300;
Mat temp(Sub_Image_Width, Sub_Image_Length, CV_8UC3);
FILE *pfile;
unsigned long int ttt = 0, x_host, y_host, pl_idx;
int x_pos, y_pos;
x_pos = wherex();
y_pos = wherey();
for (i1 = 0;i1 < n_Hor_Sub_Img;i1++)
    for (j1 = 0;j1 < n_Ver_Sub_Img;j1++)
    {
        x_host = i1;
        y_host = j1;
        sprintf(str, "Cont%lu-%lu.txt", i1, j1);
        pfile = fopen(str, "w");
        for (k = 0;k < Sub_Image_Width;k++)
        {
            for (l = 0; l < Sub_Image_Length; l++)
            {
                //puchar_ii[ii_idx + cnt] = puchar_po[po_idx + cnt];

                for (cnt = 0; cnt < n_ch; cnt++)
                {
                    temp.at<Vec3b>(k, l)[cnt] = Main_Image.at<Vec3b>(x_host, y_host)[cnt];
                    sprintf(str, "temp(%lu, %lu, %lu)= %d, IN_PO(%lu, %lu, %lu)= %d\n", k, l,
                        cnt, temp.at<Vec3b>(k, l)[cnt], x_host, y_host, cnt,
                        Main_Image.at<Vec3b>(x_host, y_host)[cnt]);
                    fputs(str, pfile);
                }
                y_host = y_host + n_Ver_Sub_Img;
                ttt++;
            }
            gotoxy(x_pos, y_pos);
            printf("%5.3f%% Completed", double(100 * ttt) / double(n_Hor_Sub_Img*n_Ver_Sub_Img*Sub_Image_Width*Sub_Image_Length));
            y_host = j1;
            x_host = x_host + n_Hor_Sub_Img;
        }
        res.push_back(temp);
        fclose(pfile);
    }
//cout << "size of ii= " << int(Main_Image.size()) << endl;
puts("\n******************************************************************");
x_pos = wherex();
y_pos = wherey();
ttt = 0;
for (i1 = 0;i1 < n_Hor_Sub_Img;i1++)
    for (j1 = 0;j1 < n_Ver_Sub_Img;j1++)
    {
        x_host = i1;
        y_host = j1;
        sprintf(str, "/error_Image_%lu-%lu.txt", i1, j1);
        pfile = fopen(str, "w");
        temp = res[j1 + (i1*n_Hor_Sub_Img)];
        for (k = 0;k < Sub_Image_Width;k++)
        {
            for (l = 0; l < Sub_Image_Length; l++)
            {
                //puchar_ii[ii_idx + cnt] = puchar_po[po_idx + cnt];
                for (cnt = 0; cnt < n_ch; cnt++)
                {
                    if (temp.at<Vec3b>(k, l)[cnt] != Main_Image.at<Vec3b>(x_host, y_host)[cnt])
                    {
                        sprintf(str, "temp(%lu, %lu, %lu)= %d, IN_PO(%lu, %lu, %lu)= %d\n", k, l,
                            cnt, temp.at<Vec3b>(k, l)[cnt], x_host, y_host, cnt,
                            Main_Image.at<Vec3b>(x_host, y_host)[cnt]);
                        fputs(str, pfile);
                    }
                }
                y_host = y_host + n_Ver_Sub_Img;
                ttt++;
            }
            gotoxy(x_pos, y_pos);
            printf("%5.3f%% Completed", double(100 * ttt) / double(n_Hor_Sub_Img*n_Ver_Sub_Img*Sub_Image_Width*Sub_Image_Length));
            y_host = j1;
            x_host = x_host + n_Hor_Sub_Img;
        }
        fclose(pfile);
    }

return res;

}

edit retag flag offensive close merge delete

Comments

1

I have never seen an O(n^5) implementation before.

eshirima gravatar imageeshirima ( 2017-12-05 14:19:22 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2017-12-05 13:37:48 -0600

berak gravatar image

updated 2017-12-05 14:13:46 -0600

It may seem a super-banal task.

indeed, it is, here's how you *should * do that:

Mat big(4800, 4800, CV_8UC3, Scalar(200,100,55)); // for demo purpose

int patchSize = 300;
vector<Mat> patches;

// make sure, it fits !
CV_Assert(big.rows % patchSize == 0);
CV_Assert(big.cols % patchSize == 0);

for (int i=0; i<big.cols; i+=patchSize) {
    for (int j=0; j<big.rows; j+=patchSize) {
        patches.push_back(big(Rect(i, j, patchSize, patchSize)));
    }
}
edit flag offensive delete link more

Comments

Thank you for your response. It was great :)

Digital Design gravatar imageDigital Design ( 2017-12-06 14:15:05 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-12-05 12:10:14 -0600

Seen: 243 times

Last updated: Dec 05 '17