![](/img/trans.png)
[英]opencv/javacv: How to iterate over contours for shape identification?
[英]How to identify and fill I shape Contours in javacv?
我正在開發關於javacv的項目,我需要知道如何識別以下圖像並使用特定顏色填充圖像?
我嘗試通過這個問題 ,這是我使用的圖像
我嘗試通過這段代碼,並在javacv中開發了一個代碼
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import java.io.File;
import javax.swing.JFileChooser;
public class PolyGonIdentification {
public static void main(String[] args) {
CanvasFrame cnvs=new CanvasFrame("Polygon");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
cvCvtColor(src, gry, CV_BGR2GRAY);
cvThreshold(gry, gry, 230, 255, CV_THRESH_BINARY_INV);
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
System.out.println(squares.total());
for (int i=0; i<squares.total(); i++)
{
cvDrawContours(gry, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8);
}
IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT, null);
cvDilate(gry, gry, mat, CV_C);
cvErode(gry, gry, mat, CV_C);
cnvs.showImage(gry);
}
}
我的最終結果應該像這張圖片
上面的代碼產生了這種形象。 有人可以幫我解決這個問題嗎?
代碼確實完成了應該做的事情:
// as stated [in the answer to your previous question][1], findContours leaves gry set to 0, or black
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
System.out.println(squares.total()); // nothing interesting
for (int i = 0; i < squares.total(); i++)
{
// draw a gray contour (127) on a black image
cvDrawContours(gry, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8);
}
要糾正它,你必須將gry設置為255(白色,在任何調用drawContours之前,並繪制黑色輪廓!即0,而不是127.)
您只需使用外部檢索模式找到輪廓:
CV_RETR_EXTERNAL retrives only the extreme outer contours
在您的情況下,這是最佳模式,因為您有一個外部輪廓,這個輪廓是您正在尋找的。 這是文檔。
在此之后,使用帶有CV_FILLED
作為厚度參數的drawContours繪制它以填充外部多邊形。
您可以使用此代碼對其進行歸檔
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import java.io.File;
import javax.swing.JFileChooser;
public class Ishape {
public static void main(String[] args) {
CanvasFrame cnvs=new CanvasFrame("Polygon");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
cvCvtColor(src, gry, CV_BGR2GRAY);
cvThreshold(gry, gry, 230, 255, CV_THRESH_BINARY_INV);
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
CvSeq ss=null;
for (int i=0; i<1; i++)
{
cvDrawContours(gry, squares, CvScalar.WHITE, CV_RGB(248, 18, 18), 1, -1, 8);
ss=cvApproxPoly(squares, sizeof(CvContour.class), storage, CV_POLY_APPROX_DP, 8, 0);
}
IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT, null);
cvDilate(gry, gry, mat, CV_C);
cvErode(gry, gry, mat, CV_C);
cnvs.showImage(gry);
}
}
結果
這將是結果,我相信這可以幫助您解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.