use*_*218 0 c++ opencv roi image-processing
我试图从二进制图像中获取白色 n 黑色像素的坐标,以便我可以获得图像的 ROI 并更改像素值。我可以调用任何函数吗?
这是原始图像

之后,将使用 OTSU 方法对其进行阈值处理。结果:图8.7
我希望从图 8.7 中得到图 8.8 的结果(对图像感到抱歉,我已经尝试旋转它,但它仍然以这种方式显示)。有什么方法吗?
这是阈值图像链接。https://i.stack.imgur.com/zKHav.png

之后我就能获得投资回报率。

干得好:
将图像细分为块/单元,并计算白色/黑色像素的比率/百分比,并将掩模图像中的整个块着色为所需的颜色。
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;
}
Run Code Online (Sandbox Code Playgroud)
输入:此处输入的是阈值指纹,如果您使用原始扫描作为输入,则必须手动阈值。

输出:
块中 > 30% 白色像素的掩码:

组合掩码和输入(输入=此处阈值化):
