[英]OpenCV returns black and white image instead of grayscale
The following is my attempt to preprocess an image. 以下是我对图像进行预处理的尝试。 That involves the followings steps
这涉及以下步骤
When I try to go back from step 3 to step 2, I get a black and white image instead of a grayscale one. 当我尝试从步骤3返回到步骤2时,得到的是黑白图像,而不是灰度图像。 The following is my code to perform preprocessing
以下是我执行预处理的代码
cv::Mat maskCrop(std::string imageName, std::string maskName)
{
cv::Mat image,mask;
image = cv::imread( imageName, CV_LOAD_IMAGE_GRAYSCALE);
mask = cv::imread( maskName,CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat final_image;
cv::resize(image, image, mask.size()); // make the size of mask and image same
cv::bitwise_and(image, mask, final_image); //Apply mask
// define rectangular window for cropping
int offset_x = 1250; // top left corner, for cropping
int offset_y = 1550;
cv::Rect roi;
roi.x = offset_x;
roi.y = offset_y;
roi.width = 550;
roi.height = 650;
// Crop the original image to the defined ROI //
cv::Mat crop = final_image(roi);
//scale the image
float beta = 1.0 / 255.0;
crop *=beta; // divide the max pixel vaue i.e. 255
//Examinig whether scaling can be undone !!!!
cv::imwrite("/home/dpk/Desktop/ip-rings/gray_image.jpg",crop*255);
return crop;
}
The following is the output that I get when I try to undo scaling by 255(step 3) 以下是我尝试撤消255缩放比例时获得的输出(第3步)
The following is the expected output 以下是预期的输出
What causes the image to be black and white instead of grayscale? 是什么导致图像为黑白而不是灰度?
It's because class of data... I assume that the crop
matrix class is uchar
and when you divide to 255 the output is 0 or 1 (every intensity between 0 to 254 is zero and 255 will be 1) and when you multipy output in 255 the output will be 0 and 255. so test this code 这是因为数据类...我假设
crop
矩阵类是uchar
,当您除以255时,输出为0或1(0到254之间的每个强度为零,而255为1),并且在255输出将是0和255。因此测试此代码
{
.
.
cv::Mat crop = final_image(roi);
crop.convertTo(crop,
CV_32F, //New Type
1.0f/255, //Scaling
0); //offset
cv::imwrite("/home/dpk/Desktop/ip-rings/gray_image.jpg",crop*255);
return crop;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.