content:
Hello there,
For a personnel projet, I'm trying to detect object and there shadow. These are the result I have for now: Original:
題,原始問題

Object:
Shadow:
The external contours of the object are quite good, but as you can see, my object is not full. Same for the shadow. I would like to get full contours, filled, for the object and its shadow, and I don't know how to get better than this (I juste use "dilate" for the moment). Does someone knows a way to obtain a better result please? Regards.
二、問題分析
從原始圖片上來看,這張圖片的拍攝的背景比較復雜,此外光照也存在偏光現象;而提問者雖然提出的是“將縫隙合並”的要求,實際上他還是想得到目標物體的准確輪廓。
三、問題解決
基於現有經驗,和OpenCV,GOCVhelper等工具,能夠很快得出以下結果
h通道:

去光差:

閾值:

標注:

四、算法關鍵
這套算法首先解決了這個問題,而且我認為也是穩健魯棒的。其中,算法中除了經典的“hsv分解->ostu閾值->最大輪廓標注”外,最為關鍵的算法為頂帽去光差。這個算法來自於岡薩雷斯《數字圖像處理教程》形態學篇章,完全按照書本建議實現,體現良好作用。
//answerOpenCV OpenCV / C++ - Filling holes
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
//find the biggest contour
vector<Point> FindBigestContour(Mat src){
int imax = 0;
int imaxcontour = -1;
std::vector<std::vector<Point> >contours;
findContours(src,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
for (int i=0;i<contours.size();i++){
int itmp = contourArea(contours[i]);
if (imaxcontour < itmp ){
imax = i;
imaxcontour = itmp;
}
}
return contours[imax];
}
//remove Light difference by using top hat
Mat moveLightDiff(Mat src,int radius){
Mat dst;
Mat srcclone = src.clone();
Mat mask = Mat::zeros(radius*2,radius*2,CV_8U);
circle(mask,Point(radius,radius),radius,Scalar(255),-1);
//top hat
erode(srcclone,srcclone,mask);
dilate(srcclone,srcclone,mask);
dst = src - srcclone;
return dst;
}
int main( void )
{
Mat src = imread("e:/sandbox/question.png");
Mat src_hsv;
Mat bin;
Mat src_h;
cvtColor(src,src_hsv,COLOR_BGR2HSV);
vector<Mat> rgb_planes;
split(src_hsv, rgb_planes );
src_h = rgb_planes[0]; // h channel is useful
src_h = moveLightDiff(src_h,40);
threshold(src_h,bin,100,255,THRESH_OTSU);
//find and draw the biggest contour
vector<Point> bigestcontrour = FindBigestContour(bin);
vector<vector<Point> > controus;
controus.push_back(bigestcontrour);
cv::drawContours(src,controus,0,Scalar(0,0,255),3);
waitKey();
return 0;
}
五、經驗小結
解決這個問題我只用了10分鍾的時間,寫博客10分鍾。能夠快速解決問題並書寫出來的關鍵為:
1、積累維護的代碼庫:GOCVHelper(https://github.com/jsxyhelu/GOCvHelper)
2、不斷閱讀思考實踐的習慣;
感謝閱讀至此,希望有所幫助!