Domanda

I'm trying to get the coordinate of the white n black pixels from a binary image so that i can get the ROI of the image and alter the pixel values. Any function that I can call?

This is the original image

After that it'll be thresholded using OTSU method. Result : Figure8.7

I wish to get the result of Figure 8.8 from Figure 8.7 (Sorry about the image, i tried to rotate it already but it still appears this way). Is there any method?

Here's the thresholded image link. http://i.imgur.com/9EXmHN0.png

After that I'll be able to get my ROI.

È stato utile?

Soluzione

Here you go:

Subdivide the image into blocks/cells and compute the ratio/percentage of white/black pixel and color the whole block in a mask image in the desired color.

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;
}

input: input here is the thresholded fingerprint, if you use your original scan as the input, you have to threshold manually.

enter image description here

output:

mask for > 30% white pixel in a block:

enter image description here

combined mask and input (input = thresholded here):

enter image description here

Altri suggerimenti

You are looking for Mat::copyTo() and use the second image as the 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.

You need to use thresholding for this. See http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html

From the looks of it, you would need binary or binary (inverted).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top