从二值图像中提取白色区域

Extract white regions from binary images

我正在尝试 select 仅从某些面部图像中分割出的眼睛。这是一张二值图像的示例。

问题是我想要获取图像的每只眼睛,将它们放在固定大小的方形图像的中心。在这种情况下,将有两个图像,眼睛以固定的 nxn 平方图像为中心。

我试图检查以下 post 但它似乎只在二进制图像的很大一部分中起作用。在我的情况下该怎么做?

基本上,你可以计算你的等高线面积并取参考面积。所以,你可以限制你想要的轮廓。然后,您可以选择您的相关投资回报率。

您可以使用以下代码:

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat src_gray;
int thresh = 100;
RNG rng(12345);
void thresh_callback(int, void *);
int main(int argc, char **argv)
{
    Mat src = imread("../input/eyes.png");
    if (src.empty())
    {
        cout << "Could not open or find the image!\n"
             << endl;
        cout << "Usage: " << argv[0] << " <Input image>" << endl;
        return -1;
    }
    cvtColor(src, src_gray, COLOR_BGR2GRAY);
    blur(src_gray, src_gray, Size(5, 5));
    const char *source_window = "Source";
    namedWindow(source_window);
    imshow(source_window, src);
    const int max_thresh = 255;
    createTrackbar("Canny thresh:", source_window, &thresh, max_thresh, thresh_callback);
    thresh_callback(0, 0);
    waitKey();
    return 0;
}
void thresh_callback(int, void *)
{
    Mat canny_output;
    Canny(src_gray, canny_output, thresh, thresh * 2);
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
    Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
    cv::Mat roi1, roi2;
    int count = 0;
    for (size_t i = 0; i < contours.size(); i++)
    {
        if (contourArea(contours[i]) > 1300)
        {
            count++;
            std::cout << " Area: " << contourArea(contours[i]) << std::endl;
            Scalar color = Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
            drawContours(drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0);
            Rect box = boundingRect(contours[i]);
            std::cout << " Box: " << box << std::endl;

            if (count == 1)
            {
                roi1 = src_gray(box);
            }
            else if (count == 2)
            {
                roi2 = src_gray(box);
            }
        }
    }

    std::cout << " " << std::endl;
    imshow("Contours", drawing);
    imshow("roi1", roi1);
    imshow("roi2", roi2);
}

我找到了所有白色区域的中心,并将 nxn 平方图像保存在这个位置。

import cv2
from matplotlib import pyplot as plt
import numpy as np

img = cv2.imread('vQZyz.png',0)

im_bin = cv2.threshold(img.copy(), 127, 255, cv2.THRESH_BINARY)[1]
num_labels, labels_im = cv2.connectedComponents(img)

n = 100 #size of the ROI
n //= 2

for i in range(1,labels_im.max()+1): #for each white area
  #find the center of the area
  x = (min(np.where(labels_im == i)[0]) + max(np.where(labels_im == i)[0]))//2
  y = (min(np.where(labels_im == i)[1]) + max(np.where(labels_im == i)[1]))//2

  ROI = img[max(0,x-n):min(x+n,img.shape[0]),
            max(0,y-n):min(y+n,img.shape[1])]
  
  cv2.imwrite("ROI"+str(i)+"_"+str(2*n)+"x"+str(2*n)+".png", ROI) #save the ROI

n = 100 的输出: