###実現したい事
Opencvを用いてこのサイトのようなことがしたいのですが、エラーが8つほど出てしまいます。サイトはC++で書かれているのですが、私はC++がよくわからないので私なりにJavaに書き換えています。解決策が分かる方回答お願いします。
###エラー文
>javac -Xdiags:verbose sample.java sample.java:27: エラー: 不適合な型: ArrayList<ArrayList<Point>>をList<List<Point>>に変換できません: List<List<Point>> contours = new ArrayList<ArrayList<Point>>();//輪郭情報を格納 ^ sample.java:28: エラー: シンボルを見つけられません List<Vec4i> hierarchy = new ArrayList<Vec4i>();//階層構造を格納 ^ シンボル: クラス Vec4i 場所: クラス sample sample.java:28: エラー: シンボルを見つけられません List<Vec4i> hierarchy = new ArrayList<Vec4i>();//階層構造を格納 ^ シンボル: クラス Vec4i 場所: クラス sample sample.java:34: エラー: staticでないメソッド minPoint(List<Point>)をstaticコンテキストから参照することはできません Point minP = minPoint(contours.get(i)); ^ sample.java:35: エラー: staticでないメソッド maxPoint(List<Point>)をstaticコンテキストから参照することはできません Point maxP = maxPoint(contours.get(i)); ^ sample.java:38: エラー: rectangleに適切なメソッドが見つかりません(Mat,Rect,Scalar,int,int) Imgproc.rectangle(src, rect, new Scalar(255, 0, 0), 2, 8); ^ メソッド Imgproc.rectangle(Mat,Point,Point,Scalar,int,int,int)は使用できません (実引数リストと仮引数リストの長さが異なります) メソッド Imgproc.rectangle(Mat,Point,Point,Scalar,int)は使用できません (引数の不一致: RectをPointに変換できません:) メソッド Imgproc.rectangle(Mat,Point,Point,Scalar)は使用できません (実引数リストと仮引数リストの長さが異なります) sample.java:53: エラー: シンボルを見つけられません return Point(minx, miny); ^ シンボル: メソッド Point(double,double) 場所: クラス findContours2 sample.java:67: エラー: シンボルを見つけられません return Point(maxx, maxy); ^ シンボル: メソッド Point(double,double) 場所: クラス sample エラー8個
###コード
Java
1import java.util.ArrayList; 2import java.util.List; 3import org.opencv.core.*; 4import org.opencv.imgcodecs.*; 5import org.opencv.highgui.Highgui; 6import org.opencv.imgproc.Imgproc; 7 8public class findContours2{ 9 10 /** 11 * 輪郭を抽出して、輪郭を囲む四角形を描画します。 12 */ 13 public static void main(String[] args){ 14 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 15 //画像読み込み 16 Mat src= Imgcodecs.imread("num.png"); 17 // グレースケール画像に変換 18 Mat grayImage; 19 Imgproc.cvtColor(src, grayImage, Imgproc.COLOR_BGR2GRAY); 20 // 二値画像に変換 21 Mat binaryImage; 22 final double threshold = 100.0; 23 final double maxValue = 255.0; 24 Imgproc.threshold(grayImage, binaryImage, threshold, maxValue, Imgproc.THRESH_BINARY| Imgproc.THRESH_OTSU); 25 Core.bitwise_not(binaryImage, binaryImage); 26 27 List<List<Point>> contours = new ArrayList<ArrayList<Point>>();//輪郭情報を格納 28 List<Vec4i> hierarchy = new ArrayList<Vec4i>();//階層構造を格納 29 //Imgproc.RETR_EXTERNAL(階層構造を持った輪郭を出力) 30 Imgproc.findContours(binaryImage, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 31 32 //各輪郭の最大最小座標を求める 33 for(int i = 0;i < contours.size(); i++){ 34 Point minP = minPoint(contours.get(i)); 35 Point maxP = maxPoint(contours.get(i)); 36 Rect rect = new Rect(minP,maxP); 37 //矩形を描く 38 Imgproc.rectangle(src, rect, new Scalar(255, 0, 0), 2, 8); 39 } 40 } 41 //最小座標を求める 42 public Point minPoint(List<Point> contours){ 43 double minx = contours.get(0).x; 44 double miny = contours.get(0).y; 45 for(int i=1;i<contours.size(); i++){ 46 if(minx > contours.get(i).x){ 47 minx = contours.get(i).x; 48 } 49 if(miny > contours.get(i).y){ 50 miny = contours.get(i).y; 51 } 52 } 53 return Point(minx, miny); 54 } 55 //最大座標を求める 56 public Point maxPoint(List<Point> contours){ 57 double maxx = contours.get(0).x; 58 double maxy = contours.get(0).y; 59 for(int i=1;i<contours.size(); i++){ 60 if(maxx > contours.get(i).x){ 61 maxx = contours.get(i).x; 62 } 63 if(maxy > contours.get(i).y){ 64 maxy = contours.get(i).y; 65 } 66 } 67 return Point(maxx, maxy); 68 } 69}
###動作環境
Windows10
OpenCV3.3.1
コンパイルはコマンドプロンプトから行なっています。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。