關於調參的一些思考
合理的參數設置,應該是基於對需要解決的問題的一些已知條件。如需要提取的線段的長度範圍,需要定位的工件的尺寸、大小(面積)、形狀,周長,矩形度,圓形度等。
分析圖像的一些角度
1.從算法上
圖像降噪,直方圖增強,二值化,頻率分析,圖像形態學,幾何信息 提取,特徵提取,等各種數學方法。 儘可能多的輸出結果。
2.從策略上
篩選出實際需要的結果。
把握需要的信息和干擾信息的本質差距。
面積、周長、矩形度、圓形度、寬長比
這裏不做具體分析,以後專門寫一篇筆記。
例1:找出汽車輪轂圓孔(從輪廓和連通域兩個角度)
原圖:
分析:
1、獲取二值圖像(選用二值化閾值或者canny算子掃描)
2、通過findContours函數尋找連通域,輪廓則是對應連通域的輪廓
3、通過minAreaRect函數獲取輪廓最小矩形框(可旋轉),利改矩形框的特徵來鎖定目標(這裏我們限制,矩形框的長寬比值在1附近,並且矩形框的寬度大於10)
4、對鎖定的輪廓,通過drawContours函數繪製輪廓(注意參數,倒數第二個填-1則爲填該改輪廓,類似漫水填充,不過漫水填充不能獲取輪廓特徵)
另一種思路:
1、獲取二值圖像(這裏爲了使圓圈內部爲白,使用反閾值)
2、利用connectedComponentsWithStats函數獲取連通域矩陣
3、通過狀態矩陣statsMat,來獲取連通域最小外接四邊形 (bounding box)的 x, y, width,height和麪 積(像素數量)
4、通過四邊形的條件來限制
思路1代碼:
int main()
{
cv::Mat srcMat = imread("D:\\opencv_picture_test\\rim.png", 1);
Mat dstMat, binMat;
cvtColor(srcMat, dstMat, COLOR_BGR2GRAY);
threshold(dstMat, binMat, 0, 255, THRESH_OTSU);
imshow("bin", binMat);
//通過findContours函數尋找連通域
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binMat, contours, RETR_LIST,CHAIN_APPROX_NONE);
//繪製輪廓,內填充
for (int i = 0; i < contours.size(); i++) {
RotatedRect rbox = minAreaRect(contours[i]);
if (fabs(rbox.size.width * 1.0 / rbox.size.height - 1) < 0.1 && rbox.size.width > 10)
drawContours(srcMat, contours, i, Scalar(0, 255, 255), -1, 8);
}
imshow("rim", srcMat);
waitKey(0);
}
二值圖:
框定圖:
思路2代碼:
int main()
{
Mat lableMat;
Mat statsMat;
Mat centerMat;
Mat srcMat = imread("D:\\opencv_picture_test\\輪廓\\rim.png", 1); //讀取灰度
Mat dstMat;
cvtColor(srcMat, srcMat, COLOR_BGR2GRAY);
//調用閾值函數
threshold(srcMat, dstMat, 120, 255,THRESH_BINARY_INV);
//腐蝕操作
//Mat element = getStructuringElement(MORPH_ELLIPSE, Size(9,9));
//morphologyEx(dstMat,dstMat, MORPH_ERODE, element);
int nComp = cv::connectedComponentsWithStats(dstMat,
lableMat,
statsMat,
centerMat,
8,
CV_32S);
for (int i = 1; i < nComp; i++)
{
cout << "pixels = " << statsMat.at<int>(i, 4) << endl;
cout << "width = " << statsMat.at<int>(i, 2) << endl;
cout << "height = " << statsMat.at<int>(i, 3) << endl;
cout << endl;
}
for (int i = 1; i < nComp; i++)
{
Rect bndbox;
bndbox.x = statsMat.at<int>(i, 0);
bndbox.y = statsMat.at<int>(i, 1);
bndbox.width = statsMat.at<int>(i, 2);
bndbox.height = statsMat.at<int>(i, 3);
if (fabs(bndbox.width * 1.0 / bndbox.height - 1) < 0.1 && bndbox.width > 30)
rectangle(srcMat, bndbox, CV_RGB(255, 255, 255), 1, 8, 0);
}
imshow("src", srcMat);
//imshow("dst", dstMat);
waitKey(0);
}
效果圖:
例2:找出芯片中間正方形物體
原圖:
分析:
1、獲取二值圖像(選用二值化閾值或者canny算子掃描)
2、通過findContours函數尋找連通域,輪廓則是對應連通域的輪廓
3、通過minAreaRect函數獲取輪廓最小矩形框(可旋轉),利改矩形框的特徵來鎖定目標(這裏我們限制,矩形框的長寬比值在1附近,並且矩形框的寬度大於10)
4、對鎖定的輪廓,通過drawContours函數繪製輪廓
5、通過輪廓外最小矩形的四個頂點座標,來繪製邊框
另一種思路:
1、獲取二值圖像(這裏爲了使圓圈內部爲白,使用反閾值)
2、利用connectedComponentsWithStats函數獲取連通域矩陣
3、通過狀態矩陣statsMat,來獲取連通域最小外接四邊形 (bounding box)的 x, y, width,height和麪 積(像素數量)
4、通過四邊形的條件來限制
思路1代碼:
////*--------------------------【練習2】矩形框-------------------------------------*/
int main()
{
cv::Mat srcMat = imread("D:\\opencv_picture_test\\輪廓\\die_on_chip.png", 1);
Mat dstMat, binMat;
cvtColor(srcMat, dstMat, COLOR_BGR2GRAY);
threshold(dstMat, binMat, 0, 255, THRESH_OTSU);
imshow("bin", binMat);
//通過findContours函數尋找連通域
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binMat, contours, RETR_LIST, CHAIN_APPROX_NONE);
//繪製輪廓
for (int i = 0; i < contours.size(); i++) {
RotatedRect rbox = minAreaRect(contours[i]);
if (fabs(rbox.size.width * 1.0 / rbox.size.height - 1) < 0.1 && rbox.size.width > 10)
{
drawContours(srcMat, contours, i, Scalar(0, 255, 255), 1, 8);
Point2f vtx[4];
rbox.points(vtx);
for (int j = 0; j < 4; ++j) {
cv::line(srcMat, vtx[j], vtx[j < 3 ? j + 1 : 0], Scalar(0, 0, 255), 3, LINE_AA);
}
}
}
imshow("die_on_chip", srcMat);
waitKey(0);
}
思路2代碼:
////*--------------------------【練習1連通域解法】-------------------------------------*/
int main()
{
Mat lableMat;
Mat statsMat;
Mat centerMat;
Mat srcMat = imread("D:\\opencv_picture_test\\輪廓\\die_on_chip.png", 1); //讀取灰度
Mat dstMat;
cvtColor(srcMat, srcMat, COLOR_BGR2GRAY);
//調用閾值函數
threshold(srcMat, dstMat, 120, 255,THRESH_BINARY);
//腐蝕操作
//Mat element = getStructuringElement(MORPH_ELLIPSE, Size(9,9));
//morphologyEx(dstMat,dstMat, MORPH_ERODE, element);
int nComp = cv::connectedComponentsWithStats(dstMat,
lableMat,
statsMat,
centerMat,
8,
CV_32S);
for (int i = 1; i < nComp; i++)
{
cout << "pixels = " << statsMat.at<int>(i, 4) << endl;
cout << "width = " << statsMat.at<int>(i, 2) << endl;
cout << "height = " << statsMat.at<int>(i, 3) << endl;
cout << endl;
}
for (int i = 1; i < nComp; i++)
{
Rect bndbox;
bndbox.x = statsMat.at<int>(i, 0);
bndbox.y = statsMat.at<int>(i, 1);
bndbox.width = statsMat.at<int>(i, 2);
bndbox.height = statsMat.at<int>(i, 3);
if (fabs(bndbox.width * 1.0 / bndbox.height - 1) < 0.2 && statsMat.at<int>(i, 4)>=1200)
rectangle(srcMat, bndbox, CV_RGB(0, 255, 255), 1, 8, 0);
}
imshow("src", srcMat);
//imshow("dst", dstMat);
waitKey(0);
}
效果圖:
例3:桌面上橘色物體
分析:
1、RGB轉HSV圖
2、將HSV通道分離,獲取三個通道值
3、對S通道進行二值化處理
4、接下來按照上面兩題的思路,找輪廓,框定。、
代碼:
//*--------------------------【練習3】矩形框-------------------------------------*/
int main()
{
cv::Mat srcMat = imread("D:\\opencv_picture_test\\輪廓\\topic1.jpg", 1);
Mat dstMat, binMat;
cvtColor(srcMat, dstMat, COLOR_BGR2HSV);
vector<Mat> channels;
split(dstMat, channels);
//namedWindow("H", WINDOW_NORMAL);//WINDOW_NORMAL允許用戶自由伸縮窗口
//imshow("H", channels.at(0));
namedWindow("S", WINDOW_NORMAL);//WINDOW_NORMAL允許用戶自由伸縮窗口
imshow("S", channels.at(1));
//namedWindow("V", WINDOW_NORMAL);//WINDOW_NORMAL允許用戶自由伸縮窗口
//imshow("V", channels.at(2));
//將S通道的圖像複製,然後處理
Mat S_Mat;
channels.at(1).copyTo(S_Mat);
//namedWindow("S", WINDOW_NORMAL);//WINDOW_NORMAL允許用戶自由伸縮窗口
//imshow("S", S_Mat);
threshold(S_Mat, binMat, 120, 255, THRESH_BINARY);
namedWindow("bin", WINDOW_NORMAL);//WINDOW_NORMAL允許用戶自由伸縮窗口
imshow("bin", binMat);
//通過findContours函數尋找連通域
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binMat, contours, RETR_LIST, CHAIN_APPROX_NONE);
//繪製輪廓
for (int i = 0; i < contours.size(); i++) {
RotatedRect rbox = minAreaRect(contours[i]);
if (fabs(rbox.size.width * 1.0 / rbox.size.height - 1) < 0.3 && rbox.size.width > 10)
{
drawContours(srcMat, contours, i, Scalar(0, 255, 255), 1, 8);
Point2f vtx[4];
rbox.points(vtx);
for (int j = 0; j < 4; ++j) {
cv::line(srcMat, vtx[j], vtx[j < 3 ? j + 1 : 0], Scalar(255, 255, 255), 2, LINE_AA);
}
}
}
namedWindow("topic1", WINDOW_NORMAL);//WINDOW_NORMAL允許用戶自由伸縮窗口
imshow("topic1", srcMat);
waitKey(0);
return 0;
}
S通道圖:
用S通道進行二值化:
框定圖:
總結
從目前來看,框定目標物體我們從輪廓和連通域都可以。
其中,利用minAreaRect函數可以獲取輪廓最小矩形框的參數值,也包括了輪廓的部分信息。
利用connectedComponentsWithStats獲取連通域,可從中獲取的連通域信息。
利用這些信息,結合矩形度、圓形度、寬長比等數學特徵則可以剔除一些不符合特徵的備選項。