หากใครกำลังมองหาการสร้างมาสก์ที่ไม่ใช่สี่เหลี่ยมแล้วนำไปใช้กับภาพ ลองดูที่นี่ :
Mat& obtainIregularROI(Mat& origImag, Point2f topLeft, Point2f topRight, Point2f botLeft, Point2f botRight){
static Mat black(origImag.rows, origImag.cols, origImag.type(), cv::Scalar::all(0));
Mat mask(origImag.rows, origImag.cols, CV_8UC1, cv::Scalar(0));
vector< vector<Point> > co_ordinates;
co_ordinates.push_back(vector<Point>());
co_ordinates[0].push_back(topLeft);
co_ordinates[0].push_back(botLeft);
co_ordinates[0].push_back(botRight);
co_ordinates[0].push_back(topRight);
drawContours( mask,co_ordinates,0, Scalar(255),CV_FILLED, 8 );
origImag.copyTo(black,mask);
return black;
}
"สีดำ" คือภาพที่ในที่สุดเราก็จะได้ผลลัพธ์โดยการครอบตัด ROI ที่ผิดปกติออกจากภาพต้นฉบับ
static Mat black(origImag.rows, origImag.cols, origImag.type(), cv::Scalar::all(0));
"mask" คือ Mat ซึ่งเตรียมใช้งานเป็นขนาดเดียวกับรูปภาพต้นฉบับและเต็มไปด้วย 0 Mat mask(origImag.rows, origImag.cols, CV_8UC1, cv::Scalar(0));
วางพิกัดในทิศทาง ANTICLOCKWISE
vector< vector<Point> > co_ordinates;
co_ordinates.push_back(vector<Point>());
co_ordinates[0].push_back(topLeft);
co_ordinates[0].push_back(botLeft);
co_ordinates[0].push_back(botRight);
co_ordinates[0].push_back(topRight);
ตอนนี้กำลังสร้างหน้ากากขึ้นมาจริงๆ
drawContours( mask,co_ordinates,0, Scalar(255),CV_FILLED, 8 );
ในตอนท้าย ให้คัดลอกส่วนที่มาสก์/ROI จากรูปภาพต้นฉบับ (origImag) และวางส่วนของ ROI จากรูปภาพต้นฉบับ (โดยใช้มาสก์) ลงในรูปภาพที่มีชื่อว่า "สีดำ"
origImag.copyTo(black,mask);
person
Spandan
schedule
24.12.2017