[英]How to get the coordinate of bright or dark pixels? OpenCV
我正在嘗試從二進制圖像中獲取白色n黑色像素的坐標,以便可以獲取圖像的ROI並更改像素值。 我可以調用任何函數嗎?
這是原始圖像
之后,將使用OTSU方法對其進行閾值處理。 結果:圖8.7
我希望從圖8.7獲得圖8.8的結果(對圖像感到抱歉,我已經嘗試過旋轉它,但是它仍然以這種方式出現)。 有什么辦法嗎?
這是閾值圖像鏈接。 http://i.imgur.com/9EXmHN0.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;
}
輸入:此處輸入的是閾值指紋,如果您使用原始掃描作為輸入,則必須手動閾值。
輸出:
塊中大於30%白色像素的蒙版:
組合的掩碼和輸入(此處輸入=閾值):
您正在尋找Mat::copyTo()
並將第二張圖像用作mask
。
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.
您需要為此使用閾值。 參見http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html
從外觀上看,您將需要二進制或二進制(取反)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.