gpt4 book ai didi

c++ - 访问 Mat 中出错的图像的像素

转载 作者:行者123 更新时间:2023-12-02 16:50:24 24 4
gpt4 key购买 nike

我正在实现一个简单的去马赛克算法。我认为我以正确的方式访问像素,但输出显示我以错误的方式访问它。我应该把它作为输出
desired-output
但相反,我得到了这个
output_from_my logic
它在左上角放大。我的代码粘贴在下面

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 ;
}
我假设在访问像素时,行和列在 pixel_in = input.at<float>(row, col); 处互换。 .这个假设是基于理想和输出的图像。
为什么会放大左上角?

最佳答案

问题在于 imread() 的使用。 ,
即使图像是单 channel 的,imread 默认情况下将其读取为 RGB 图像。
当我使用标志 -1 时,这表明应该按照图像指定的方式读取数据,它工作得很好。

imread("path_to_image",-1);
感谢@Micka 提出解决方案。
干杯

关于c++ - 访问 Mat 中出错的图像的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64303868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com