Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV返回黑白图像,而不是灰度图像_C++_Opencv_Grayscale - Fatal编程技术网

C++ OpenCV返回黑白图像,而不是灰度图像

C++ OpenCV返回黑白图像,而不是灰度图像,c++,opencv,grayscale,C++,Opencv,Grayscale,下面是我对图像进行预处理的尝试。这包括以下步骤 戴上口罩 裁切结果 最后,将像素值缩放255 当我尝试从第3步返回到第2步时,我得到的是黑白图像,而不是灰度图像。 下面是我执行预处理的代码 cv::Mat maskCrop(std::string imageName, std::string maskName) { cv::Mat image,mask; image = cv::imread( imageName, CV_LOAD_IMAGE_GRAYSCALE); ma

下面是我对图像进行预处理的尝试。这包括以下步骤

  • 戴上口罩
  • 裁切结果
  • 最后,将像素值缩放255
  • 当我尝试从第3步返回到第2步时,我得到的是黑白图像,而不是灰度图像。 下面是我执行预处理的代码

    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)

    以下是预期的输出


    是什么导致图像变成黑白而不是灰度?

    这是因为数据类别。。。我假设
    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;
    }
    

    这是因为一类数据。。。我假设
    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;
    }
    

    裁剪
    矩阵的数据类型是无符号8位整数。将其乘以浮点标量不会改变这一点。
    crop
    矩阵的数据类型是无符号8位整数。将其乘以浮点标量不会改变这一点。