Reputation: 63
I am implementing a simple demosaicing algorithm. I think I am accessing a pixel in the right way, but the output is showing that I am accessing it in the wrong way. I was supposed to get this as an output
but instead, I am getting this
It is zooming in on the left top corner. My code is pasted below
pixel demosiac_interpol(float win[3][3], int r, int c)
{
pixel new_pix;
if (r % 2 == 0 && c % 2 == 0)
{
new_pix.r = (win[1][0] + win[1][2]) / 2;
new_pix.g = win[1][1] * 2;
new_pix.b = (win[0][1] + win[2][1]) / 2;
}
else if (r % 2 == 0 && c % 2 == 1)
{
new_pix.r = win[1][1];
new_pix.g = (win[1][0] + win[2][1] + win[0][1] + win[1][2]) / 2;
new_pix.b = (win[2][0] + win[0][2] + win[2][2] + win[0][0]) / 4;
}
else if (r % 2 == 1 && c % 2 == 0)//blue
{
new_pix.r = (win[0][2] + win[2][0] + win[2][2] + win[0][0]) / 4;
new_pix.g = (win[1][0] + win[1][2] + win[2][1] + win[2][1]) / 2;
new_pix.b = win[1][1];
}
else
{
new_pix.r = (win[0][1] + win[2][1]) / 2;
new_pix.g = win[1][1] * 2;
new_pix.b = (win[1][0] + win[1][2]) / 2;
}
return new_pix;
}
int main(int argc, char** argv)
{
Mat input = imread("C:/Users/20181217/image_data/raw_rc_3.png");//262x518;rowsxcols
cout << endl << input.rows << "," << input.cols;
//cout << (int)input.at<uchar>(200, 200) << endl;
//imwrite("C:/Users/20181217/image_data/raw_rc_3.png", input);
input.convertTo(input, CV_32FC1);
input = input / 256.0;
cout << (int)input.at<float>(200, 200) << endl;
Mat final_img(input.rows - 2, input.cols - 2, CV_8UC3);//260x516; size
cout <<endl<< final_img.rows << "," << final_img.cols;
const int height = 262;
const int width = 518;
float window[3][3];
float line_buf[2][width];
float pixel_in;
pixel out_pix;
for (int row = 0; row < height; row++)
{
//cout <<endl<< row<<endl;
for (int col = 0; col < width; col++)
{
//cout << col << ",";
//if(row>1 && col> 512)
//cout << "1,";
pixel_in = input.at<float>(row, col);
if (row == 200 && col == 200)
cout <<endl<< "at " << row << "and" << col << ", value is:" << pixel_in<<endl;
if (row == 261 && col == 517)
{
cout << "printing at 261,517";
cout << endl << pixel_in;
}
for (int i = 0; i < 3; i++)
{
window[i][0] = window[i][1];
window[i][1] = window[i][2];
}
window[0][2] = line_buf[0][col];
window[1][2] = line_buf[0][col] = line_buf[1][col];
window[2][2] = line_buf[1][col] = pixel_in;
if (row == 200 && col == 200)
display_win(window);
if (row > 1 && col > 1 && row < height && col < width)
{
out_pix = demosiac_interpol(window, row - 2, col - 2);
final_img.at<Vec3b>(row - 2, col - 2)[0] = uchar(fmax(fmin((out_pix.r *255.0), 255.0), 0.0));
final_img.at<Vec3b>(row - 2, col - 2)[1] = uchar(fmax(fmin((out_pix.g *255.0), 255.0), 0.0));
final_img.at<Vec3b>(row - 2, col - 2)[2] = uchar(fmax(fmin((out_pix.b *255.0), 255.0), 0.0));
}
}//cout << endl<< row ;
}
I am assuming that while accessing the pixel the row and col is getting interchanged at pixel_in = input.at<float>(row, col);
. This assumption is based on the images of ideal and output.
Why is it zooming the top left corner?
Upvotes: 0
Views: 55
Reputation: 63
The problem was with the usage of imread()
,
even though, the image was single channel one, imread by default read it as an RGB image. when I used the flag -1, which suggests the data should be read as specified by the image, it worked perfectly.
imread("path_to_image",-1);
thanks to @Micka for suggesting the solution.
cheers
Upvotes: 3