下面是我对图像进行预处理的尝试。这涉及到以下步骤
当我试图从第三步回到第二步时,我会得到一个黑白图像,而不是灰度图像。下面是我执行预处理的代码
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;
}
下面是当我试图按255撤消缩放时得到的输出(步骤3)
以下是预期的输出
是什么导致图像是黑白的而不是灰度的?
发布于 2017-07-29 12:08:42
因为这类数据..。假设crop
矩阵类是uchar
,当除以255时,输出为0或1(0到254之间的每个强度为0,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;
}
https://stackoverflow.com/questions/45393464
复制相似问题