Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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_Image Processing_Roi - Fatal编程技术网

C++ 如何获得亮像素或暗像素的坐标?OpenCV

C++ 如何获得亮像素或暗像素的坐标?OpenCV,c++,opencv,image-processing,roi,C++,Opencv,Image Processing,Roi,我试图从二值图像中得到黑白像素的坐标,这样我就可以得到图像的ROI并改变像素值。有我可以调用的函数吗 这是原始图像 之后,将使用大津方法对其进行阈值化。结果:图8.7 我希望从图8.7中得到图8.8的结果(很抱歉,我已经尝试旋转它,但它仍然以这种方式显示)。有什么办法吗 这是阈值图像链接 之后,我将能够获得我的投资回报率。 您正在查找并使用第二个图像作为掩码 C++: void Mat::copyTo(OutputArray m, InputArray mask) const Parame

我试图从二值图像中得到黑白像素的坐标,这样我就可以得到图像的ROI并改变像素值。有我可以调用的函数吗

这是原始图像

之后,将使用大津方法对其进行阈值化。结果:图8.7

我希望从图8.7中得到图8.8的结果(很抱歉,我已经尝试旋转它,但它仍然以这种方式显示)。有什么办法吗

这是阈值图像链接

之后,我将能够获得我的投资回报率。

您正在查找并使用第二个图像作为
掩码

C++: void Mat::copyTo(OutputArray m, InputArray mask) const

Parameters: 
    m – Destination matrix. If it does not have a proper size or type before the operation, it is reallocated.
    mask – Operation mask. Its non-zero elements indicate which matrix elements need to be copied.
您正在查找并使用第二个图像作为
遮罩

C++: void Mat::copyTo(OutputArray m, InputArray mask) const

Parameters: 
    m – Destination matrix. If it does not have a proper size or type before the operation, it is reallocated.
    mask – Operation mask. Its non-zero elements indicate which matrix elements need to be copied.

您需要为此使用阈值。看


从外观上看,您需要二进制或二进制(反转)。

您需要使用阈值。看

从外观上看,您需要二进制或二进制(反转)。

给您:

将图像细分为块/单元,计算白/黑像素的比率/百分比,并将遮罩图像中的整个块按所需颜色着色

int main()
{
// load your real image here
cv::Mat img = cv::imread("fingerprint.png", CV_LOAD_IMAGE_GRAYSCALE);


// after thresholding: all pixel 255 or 0
cv::Mat thres = img > 0;    // input image was thresholded already...
//cv::threshold(img,thres,58,255,CV_THRESH_OTSU);   // if original input, use your OTSU (remark: have to convert to grayscale first?)

cv::Mat mask = thres.clone();
mask = 100; // set it to gray to see at the end whether all blocks were performed and painted

//float minRatio = 0.5f;
float minRatio = 0.3f;
//float minRatio = 0.1f;    // ratio of white pixel within a block to accept as a filled block

// size of a single block:
cv::Size block(16,26);

// count pixel in each block and decide whether the block is white or black:
for(int j=0; j<img.rows; j+=block.height)
    for(int i=0; i<img.cols; i+=block.width)
    {
        // current block:
        cv::Rect currentBlock(i, j, block.width, block.height);

        // pixel counter
        unsigned int cWhite = 0;
        unsigned int cBlack = 0;
        // iterate through whole block and count pixels
        for(int y=currentBlock.y; y<currentBlock.y+currentBlock.height; ++y)
            for(int x=currentBlock.x; x<currentBlock.x+currentBlock.height; ++x)
            {
                // care for blocks that don't fit into the image. If known imagesize and block sizes fit exactly, this may be removed
                if((y < img.rows)&&(x < img.cols))
                {
                    if(thres.at<unsigned char>(y,x) == 255) cWhite++;
                    else cBlack++;
                }
            }

        // compute block color from ratio
        unsigned char blockColor = 0;
        if((float)cWhite/(float)(cBlack+cWhite) > minRatio) blockColor = 255;

        // same loop as before, but now fill the mask. maybe there are faster ways... don't know
        for(int y=currentBlock.y; y<currentBlock.y+currentBlock.height; ++y)
            for(int x=currentBlock.x; x<currentBlock.x+currentBlock.height; ++x)
            {
                if((y < img.rows)&&(x < img.cols))
                {
                    mask.at<unsigned char>(y,x) = blockColor;   // set mask block color
                }
            }
    }

// copy the image masked
cv::Mat combined;
img.copyTo(combined,mask);


// writing results to show you
cv::imwrite("fingerprintInput.png", thres);

cv::imshow("mask",mask);
cv::imwrite("fingerprintMask.png", mask);

cv::imshow("combined", combined);
cv::imwrite("fingerprintCombined.png", combined);


cv::waitKey(-1);
return 0;
}
intmain()
{
//在这里加载您的真实图像
cv::Mat img=cv::imread(“fingerprint.png”,cv\u LOAD\u IMAGE\u GRAYSCALE);
//阈值化后:所有像素255或0
cv::Mat thres=img>0;//输入图像已设置阈值。。。
//cv::阈值(img,thres,58255,cv_THRESH_OTSU);//如果是原始输入,请使用您的OTSU(备注:必须先转换为灰度?)
cv::Mat mask=thres.clone();
mask=100;//将其设置为灰色,以便在最后查看是否执行并绘制了所有块
//浮点数比=0.5f;
浮动比率=0.3f;
//float minRatio=0.1f;//块内白色像素接受为填充块的比率
//单个块的大小:
cv::尺寸块(16,26);
//计算每个块中的像素数并确定块是白色还是黑色:
对于(int j=0;j

输出:

块中大于30%白色像素的遮罩:

组合遮罩和输入(此处输入=阈值):

给你:

将图像细分为块/单元,计算白/黑像素的比率/百分比,并将遮罩图像中的整个块按所需颜色着色

int main()
{
// load your real image here
cv::Mat img = cv::imread("fingerprint.png", CV_LOAD_IMAGE_GRAYSCALE);


// after thresholding: all pixel 255 or 0
cv::Mat thres = img > 0;    // input image was thresholded already...
//cv::threshold(img,thres,58,255,CV_THRESH_OTSU);   // if original input, use your OTSU (remark: have to convert to grayscale first?)

cv::Mat mask = thres.clone();
mask = 100; // set it to gray to see at the end whether all blocks were performed and painted

//float minRatio = 0.5f;
float minRatio = 0.3f;
//float minRatio = 0.1f;    // ratio of white pixel within a block to accept as a filled block

// size of a single block:
cv::Size block(16,26);

// count pixel in each block and decide whether the block is white or black:
for(int j=0; j<img.rows; j+=block.height)
    for(int i=0; i<img.cols; i+=block.width)
    {
        // current block:
        cv::Rect currentBlock(i, j, block.width, block.height);

        // pixel counter
        unsigned int cWhite = 0;
        unsigned int cBlack = 0;
        // iterate through whole block and count pixels
        for(int y=currentBlock.y; y<currentBlock.y+currentBlock.height; ++y)
            for(int x=currentBlock.x; x<currentBlock.x+currentBlock.height; ++x)
            {
                // care for blocks that don't fit into the image. If known imagesize and block sizes fit exactly, this may be removed
                if((y < img.rows)&&(x < img.cols))
                {
                    if(thres.at<unsigned char>(y,x) == 255) cWhite++;
                    else cBlack++;
                }
            }

        // compute block color from ratio
        unsigned char blockColor = 0;
        if((float)cWhite/(float)(cBlack+cWhite) > minRatio) blockColor = 255;

        // same loop as before, but now fill the mask. maybe there are faster ways... don't know
        for(int y=currentBlock.y; y<currentBlock.y+currentBlock.height; ++y)
            for(int x=currentBlock.x; x<currentBlock.x+currentBlock.height; ++x)
            {
                if((y < img.rows)&&(x < img.cols))
                {
                    mask.at<unsigned char>(y,x) = blockColor;   // set mask block color
                }
            }
    }

// copy the image masked
cv::Mat combined;
img.copyTo(combined,mask);


// writing results to show you
cv::imwrite("fingerprintInput.png", thres);

cv::imshow("mask",mask);
cv::imwrite("fingerprintMask.png", mask);

cv::imshow("combined", combined);
cv::imwrite("fingerprintCombined.png", combined);


cv::waitKey(-1);
return 0;
}
intmain()
{
//在这里加载您的真实图像
cv::Mat img=cv::imread(“fingerprint.png”,cv\u LOAD\u IMAGE\u GRAYSCALE);
//阈值化后:所有像素255或0
cv::Mat thres=img>0;//输入图像已设置阈值。。。
//cv::阈值(img,thres,58255,cv_THRESH_OTSU);//如果是原始输入,请使用您的OTSU(备注:必须先转换为灰度?)
cv::Mat mask=thres.clone();
mask=100;//将其设置为灰色,以便在最后查看是否执行并绘制了所有块
//浮点数比=0.5f;
浮动比率=0.3f;
//float minRatio=0.1f;//块内白色像素接受为填充块的比率
//单个块的大小:
cv::尺寸块(16,26);
//计算每个块中的像素数并确定块是白色还是黑色:
对于(int j=0;j

输出:

块中大于30%白色像素的遮罩:

组合遮罩和输入(此处输入=阈值):


谢谢,这将给出图2的结果(图8.9)。但是第一张图像呢?在图8.8中有没有获得结果的快捷方式?或者我必须使用cvGetReal2d函数筛选每个像素值吗?@user3396218不是图8.8中的输入图像吗?嗨。可能我的问题不够清楚。我已经更新了我的问题。谢谢,这将给出图2(图8.9)的结果.但是第一张图像呢?是否有任何快捷方式可以获得图8.8中的结果?或者我必须使用cvGetReal2d函数筛选每个像素值吗?@user3396218不是图8.8中的输入图像吗?嗨。可能我的问题不够清楚。我已经更新了我的问题。不完全是图8.8中的内容,但你可以尝试一个大的中值滤波器在阈值图像上!为了更好地显示图8.8所示的内容,我会尝试将图像细分为大小为x²的方形单元格,然后在每个单元格中计算是否有更多的白色或黑色像素,并为整个单元格着色(在遮罩/roi图像中)顺便说一句:如果您提供一个阈值图像样本,您甚至可以尝试为您实现它;)@米卡,我正在制作一个16x26像素的遮罩并计算白色像素的百分比。但是这可能需要一段时间。顺便说一句,图像是320x480。这是图像!如果你愿意帮忙的话,我将不胜感激。谢谢!顺便说一句,OTSU=58的阈值。这与图8.8中的不完全一样,但是你可以尝试在图8.8中使用一个大的中值滤波器重新保存的图像!为了更好地显示图8.8所示的内容,我会尝试将您的图像细分为x²大小的方形单元,然后在每个单元中计算是否有更多的白色或黑色像素,并将整个单元(在遮罩/roi图像中)涂成该颜色。顺便说一句:如果您提供阈值图像样本,人们甚至可以尝试为您实现该功能;)@米卡,我正在制作一个16x26像素的遮罩并计算白色像素的百分比。但是这可能需要一些时间。顺便说一句,图像是320x480。这是图像!如果你愿意帮忙,我将不胜感激。谢谢!顺便说一句,OTSU=58.OMG的阈值。我刚回来。非常感谢你的帮助!我会在2个月后尝试它我醒了。你是救命恩人!!!!天哪。我刚回来。非常感谢你的帮助!我醒后会试试的。你是救命恩人!!!!