teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

実行画面を追加しました

2019/11/12 04:39

投稿

SKMT
SKMT

スコア57

title CHANGED
File without changes
body CHANGED
@@ -69,6 +69,18 @@
69
69
 
70
70
  ```
71
71
 
72
+ ###実行結果
73
+ cv::Point2f src[4]; // 変換元
74
+ cv::Point2f dst[4]; // 変換先
75
+ cv::Mat perspective_matrix = cv::getPerspectiveTransform(src, dst);
76
+ cv::warpPerspective(gray, gray, perspective_matrix, gray.size(), cv::INTER_LINEAR);
77
+
78
+ 以外の実行結果です
79
+ 上の4行まで実行すると多分この真ん中の黒い矩形を検出してると思うのですが、
80
+ ただの真っ黒い画像になってしまいます。
81
+
82
+ ![イメージ説明](24b884befaf055bb4a24914740452552.png)
83
+
72
84
  ### 試したこと
73
85
  ・https://dev.classmethod.jp/smartphone/avfoundation-opencv-findcontours/
74
86
  ・https://dev.classmethod.jp/smartphone/ios-avfoundationavcapturevideodataoutput-opencv/

2

問題を具体的に記入した。

2019/11/12 04:39

投稿

SKMT
SKMT

スコア57

title CHANGED
File without changes
body CHANGED
@@ -6,6 +6,7 @@
6
6
  ### 発生している問題・エラーメッセージ
7
7
  矩形検出の部分で二値化して輪郭検出することができたのですが、参考にしているサイトのように
8
8
  元画像に赤枠で検出することが出来ません
9
+ 真っ黒に白で矩形検出した画像になってしまいます
9
10
 
10
11
  ### 該当のソースコード
11
12
 

1

コードを更新して少しマシになりました

2019/11/06 07:37

投稿

SKMT
SKMT

スコア57

title CHANGED
@@ -1,1 +1,1 @@
1
- [swift]openCVを用いた画像処理アプリの作成
1
+ openCVを用いた画像処理アプリの作成
body CHANGED
@@ -1,104 +1,71 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- openCVを使って矩形検出するアプリを作りたいです。
3
+ openCVを使って矩形検出するiPhone用アプリを作りたいです。
4
- 最終的には名詞読み取りアプリのようにしたいのですが、
5
- 画像処理の部分で躓いていて中々進みません。
6
4
 
5
+
7
6
  ### 発生している問題・エラーメッセージ
7
+ 矩形検出の部分で二値化して輪郭検出することができたのですが、参考にしているサイトのように
8
- コンパイルは通ったのですが、矩形が検されません
8
+ 元画像に赤枠検出ることが出ません
9
- switchを使ってimageviewの画像を変換しようとしてますがうまくいきません。
10
9
 
11
10
  ### 該当のソースコード
12
11
 
13
- ###viewcontroller.swift
14
- ```
15
- import UIKit
16
-
17
- class ViewController: UIViewController {
18
-
19
- let openCV = openCVer()
20
-
21
- override func viewDidLoad() {
22
- super.viewDidLoad()
23
- // Do any additional setup after loading the view, typically from a nib.
24
- }
25
-
26
- override func didReceiveMemoryWarning() {
27
- super.didReceiveMemoryWarning()
28
- // Dispose of any resources that can be recreated.
29
- }
30
-
31
- @IBOutlet weak var setView: UIImageView!
32
-
33
- @IBAction func setSwitch(_ sender: Any) {
34
- // UIImageViewに表示するUIImage
35
- let gray_img : UIImage!
36
- // gray_imgにグレースケール化したimageを入れる
37
- gray_img = openCV.filter(setView.image) //---①
38
- // imageにgray_imageを表示
39
- setView.image = gray_img //---②
40
- }
41
-
42
- }
43
- ```
44
-
45
- ###openCVer.h
12
+ ```objectiveC
46
-
47
- ```
48
- #import <Foundation/Foundation.h>
49
- #import <UIKit/UIKit.h>
50
-
51
- @interface openCVer : NSObject
52
-
53
- - (UIImage *)Filter:(UIImage *)image;
54
-
55
- @property bool useBlur;
56
- @property int blur0;
57
- @property int blur1;
58
- @property bool useTreshold;
59
- @property bool useAdaptiveTreshold;
60
- @property int adaptiveThreshold0;
61
- @property int adaptiveThreshold1;
62
-
63
-
64
- @end
65
- ```
66
-
67
- ###openCVer.mm
68
- ```
69
- #import "openCVer.h"
70
13
  #import "opencv2/opencv.hpp"
71
14
  #import "opencv2/highgui/ios.h"
15
+ #import "openCVer.h"
72
16
 
73
17
  @implementation openCVer
74
18
 
75
- -(UIImage *)Filter:(UIImage *)image {
19
+ -(UIImage *)toGray:(UIImage *)input_img {
20
+ // 変換用Matの宣言
21
+ cv::Mat gray;
22
+ // input_imageをcv::Mat型へ変換
23
+ UIImageToMat(input_img, gray); //---②
76
24
 
77
- cv::Mat mat;
78
- UIImageToMat(image, mat);
79
-
80
- cv::Mat gray; // グレースケール出力用
81
- cv::cvtColor(mat,gray,CV_BGR2GRAY);
25
+ cv::cvtColor(gray, gray, CV_BGR2GRAY); //---③
82
-
83
26
  cv::threshold(gray, gray, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
84
-
27
+
85
- cv::threshold(gray, gray, 200, 255, CV_THRESH_TOZERO_INV );
28
+ // cv::threshold(gray, gray, 200, 255, CV_THRESH_TOZERO_INV );
86
- cv::bitwise_not(gray, gray); // 白黒の反転
29
+ // cv::bitwise_not(gray, gray); // 白黒の反転
87
30
  cv::threshold(gray, gray, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
88
-
31
+
89
32
  std::vector<std::vector<cv::Point>> contours;
90
33
  std::vector<cv::Vec4i> hierarchy;
91
34
  cv::findContours(gray, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_TC89_L1);
92
-
35
+
93
36
  int max_level = 0;
94
37
  for(int i = 0; i < contours.size(); i++){
95
- cv::drawContours(mat, contours, i, cv::Scalar(255, 0, 0, 255), 3, CV_AA, hierarchy, max_level);
38
+ cv::drawContours(gray, contours, i, cv::Scalar(255, 0, 0, 255), 3, CV_AA, hierarchy, max_level);
96
39
  }
97
40
 
41
+ max_level = 0;
42
+
43
+ for(int i = 0; i < contours.size(); i++) {
44
+ // ある程度の面積が有るものだけに絞る
45
+ double a = contourArea(contours[i],false);
46
+ if(a > 15000) {
47
+ //輪郭を直線近似する
48
+ std::vector<cv::Point> approx;
49
+ cv::approxPolyDP(cv::Mat(contours[i]), approx, 0.01 * cv::arcLength(contours[i], true), true);
50
+ // 矩形のみ取得
51
+ if (approx.size() == 4) {
52
+ cv::drawContours(gray, contours, i, cv::Scalar(255, 0, 0, 255), 3, CV_AA, hierarchy, max_level);
53
+ }
54
+ }
55
+ }
56
+
57
+ cv::Point2f src[4]; // 変換元
58
+ cv::Point2f dst[4]; // 変換先
59
+ cv::Mat perspective_matrix = cv::getPerspectiveTransform(src, dst);
60
+ cv::warpPerspective(gray, gray, perspective_matrix, gray.size(), cv::INTER_LINEAR);
61
+
98
- return MatToUIImage(mat);
62
+ input_img = MatToUIImage(gray); //---④
63
+
64
+ return input_img; //---⑤
99
65
  }
100
66
 
101
67
  @end
68
+
102
69
  ```
103
70
 
104
71
  ### 試したこと