我試圖只選擇從一些面部影像中分割出來的眼睛。這是一張二值影像的示例。

問題是我想獲得影像的每只眼睛,將它們放在固定大小的平方影像中。在這種情況下,將有兩個影像,其中眼睛位于一個固定的 nxn 平方影像上。
我試圖查看以下帖子

uj5u.com熱心網友回復:
基本上,您可以計算輪廓區域并獲取參考區域。所以,你可以限制你想要的輪廓。之后,您可以選擇相關的投資回報率。
您可以使用以下代碼:
#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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/383706.html
上一篇:如何在flutter中使用image_picker的pickMultiImage選擇多張圖片后獲取圖片路徑
下一篇:將繪圖描述轉換為位圖的快速方法?
