【发布时间】:2020-01-24 21:12:05
【问题描述】:
我正在学习 open cv,我编写了一个代码,应该为图像添加盐和纸张噪声(它将一些随机像素的值变为 255),我的图像大小是 256256。问题是我无法访问所有像素,代码只改变了图像像素的一半。例如,我所说的图像大小是 256256,当我更改位于 256*256 中的像素值时,它会更改中心图像的像素。
inline cv::Mat salt(cv::Mat, int); //my function
int main()
{
int present, imagepixel;
cv::Mat image = cv::imread("sample.jpg");
imagepixel = image.rows * image.cols;
std::cout << "enter the value of salt and paper :";
std::cin >> percent;
present = (present * imagepixel) / 100; //image pixel is number of image's pixels
if (image.empty())
{
std::cout << "image is not valid" << std::endl;
}
image = salt(image, present);
cv::imshow("salt&paper", image);
cv::waitKey(0);
return 0;
}
cv::Mat salt(cv::Mat image, int present)
{
int row, col, j = 0;
srand(time(NULL));
for (int i = 0; i < present; i++)
{
row = rand() % image.rows;
col = rand() % image.cols;
image.at<unsigned char>(row, col) = 255;
}
return image;
}
-
为什么图像的行和列比使用
image.rows和image.cols之类的函数计算的要多(我在调试模式和图像中将值赋予图像,其值超出了 image.rows 和 image.cols 范围)? -
为什么输出设置为无符号字符?为什么像素的值不能是整数? (我以为
image.at<unsigned char>, <unsigned char>里面是输出类型(int, char, float)) -
为什么这段代码不能访问所有像素? (截图有加看输出)
【问题讨论】: