質問編集履歴

3

実行画面を追加しました

2019/11/12 04:39

投稿

SKMT
SKMT

スコア57

test CHANGED
File without changes
test CHANGED
@@ -140,6 +140,30 @@
140
140
 
141
141
 
142
142
 
143
+ ###実行結果
144
+
145
+ cv::Point2f src[4]; // 変換元
146
+
147
+ cv::Point2f dst[4]; // 変換先
148
+
149
+ cv::Mat perspective_matrix = cv::getPerspectiveTransform(src, dst);
150
+
151
+ cv::warpPerspective(gray, gray, perspective_matrix, gray.size(), cv::INTER_LINEAR);
152
+
153
+
154
+
155
+ 以外の実行結果です
156
+
157
+ 上の4行まで実行すると多分この真ん中の黒い矩形を検出してると思うのですが、
158
+
159
+ ただの真っ黒い画像になってしまいます。
160
+
161
+
162
+
163
+ ![イメージ説明](24b884befaf055bb4a24914740452552.png)
164
+
165
+
166
+
143
167
  ### 試したこと
144
168
 
145
169
  ・https://dev.classmethod.jp/smartphone/avfoundation-opencv-findcontours/

2

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

2019/11/12 04:39

投稿

SKMT
SKMT

スコア57

test CHANGED
File without changes
test CHANGED
@@ -13,6 +13,8 @@
13
13
  矩形検出の部分で二値化して輪郭検出することができたのですが、参考にしているサイトのように
14
14
 
15
15
  元画像に赤枠で検出することが出来ません
16
+
17
+ 真っ黒に白で矩形検出した画像になってしまいます
16
18
 
17
19
 
18
20
 

1

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

2019/11/06 07:37

投稿

SKMT
SKMT

スコア57

test CHANGED
@@ -1 +1 @@
1
- [swift]openCVを用いた画像処理アプリの作成
1
+ openCVを用いた画像処理アプリの作成
test CHANGED
@@ -2,19 +2,17 @@
2
2
 
3
3
 
4
4
 
5
- openCVを使って矩形検出するアプリを作りたいです。
5
+ openCVを使って矩形検出するiPhone用アプリを作りたいです。
6
6
 
7
- 最終的には名詞読み取りアプリのようにしたいのですが、
8
7
 
9
- 画像処理の部分で躓いていて中々進みません。
10
8
 
11
9
 
12
10
 
13
11
  ### 発生している問題・エラーメッセージ
14
12
 
15
- コンパイルは通ったのですが、矩形が検出されません。
13
+ 矩形検出の部分で二値化して輪郭検出することができたのですが、参考にしているサイトのように
16
14
 
17
- switchを使ってimageviewの画像を変換しようしてますくいきません
15
+ 画像に赤枠で検出することが出来ません
18
16
 
19
17
 
20
18
 
@@ -22,123 +20,13 @@
22
20
 
23
21
 
24
22
 
25
- ###viewcontroller.swift
26
-
27
- ```
28
-
29
- import UIKit
30
-
31
-
32
-
33
- class ViewController: UIViewController {
34
-
35
-
36
-
37
- let openCV = openCVer()
38
-
39
-
40
-
41
- override func viewDidLoad() {
42
-
43
- super.viewDidLoad()
44
-
45
- // Do any additional setup after loading the view, typically from a nib.
46
-
47
- }
48
-
49
-
50
-
51
- override func didReceiveMemoryWarning() {
52
-
53
- super.didReceiveMemoryWarning()
54
-
55
- // Dispose of any resources that can be recreated.
56
-
57
- }
58
-
59
-
60
-
61
- @IBOutlet weak var setView: UIImageView!
62
-
63
-
64
-
65
- @IBAction func setSwitch(_ sender: Any) {
66
-
67
- // UIImageViewに表示するUIImage
68
-
69
- let gray_img : UIImage!
70
-
71
- // gray_imgにグレースケール化したimageを入れる
72
-
73
- gray_img = openCV.filter(setView.image) //---①
74
-
75
- // imageにgray_imageを表示
76
-
77
- setView.image = gray_img //---②
78
-
79
- }
80
-
81
-
82
-
83
- }
84
-
85
- ```
86
-
87
-
88
-
89
- ###openCVer.h
23
+ ```objectiveC
90
-
91
-
92
-
93
- ```
94
-
95
- #import <Foundation/Foundation.h>
96
-
97
- #import <UIKit/UIKit.h>
98
-
99
-
100
-
101
- @interface openCVer : NSObject
102
-
103
-
104
-
105
- - (UIImage *)Filter:(UIImage *)image;
106
-
107
-
108
-
109
- @property bool useBlur;
110
-
111
- @property int blur0;
112
-
113
- @property int blur1;
114
-
115
- @property bool useTreshold;
116
-
117
- @property bool useAdaptiveTreshold;
118
-
119
- @property int adaptiveThreshold0;
120
-
121
- @property int adaptiveThreshold1;
122
-
123
-
124
-
125
-
126
-
127
- @end
128
-
129
- ```
130
-
131
-
132
-
133
- ###openCVer.mm
134
-
135
- ```
136
-
137
- #import "openCVer.h"
138
24
 
139
25
  #import "opencv2/opencv.hpp"
140
26
 
141
27
  #import "opencv2/highgui/ios.h"
28
+
29
+ #import "openCVer.h"
142
30
 
143
31
 
144
32
 
@@ -146,33 +34,31 @@
146
34
 
147
35
 
148
36
 
149
- -(UIImage *)Filter:(UIImage *)image {
37
+ -(UIImage *)toGray:(UIImage *)input_img {
38
+
39
+ // 変換用Matの宣言
40
+
41
+ cv::Mat gray;
42
+
43
+ // input_imageをcv::Mat型へ変換
44
+
45
+ UIImageToMat(input_img, gray); //---②
150
46
 
151
47
 
152
48
 
153
- cv::Mat mat;
154
-
155
- UIImageToMat(image, mat);
156
-
157
-
158
-
159
- cv::Mat gray; // グレースケール出力用
160
-
161
- cv::cvtColor(mat,gray,CV_BGR2GRAY);
49
+ cv::cvtColor(gray, gray, CV_BGR2GRAY); //---③
162
-
163
-
164
50
 
165
51
  cv::threshold(gray, gray, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
166
52
 
53
+
167
54
 
55
+ // cv::threshold(gray, gray, 200, 255, CV_THRESH_TOZERO_INV );
168
56
 
169
- cv::threshold(gray, gray, 200, 255, CV_THRESH_TOZERO_INV );
170
-
171
- cv::bitwise_not(gray, gray); // 白黒の反転
57
+ // cv::bitwise_not(gray, gray); // 白黒の反転
172
58
 
173
59
  cv::threshold(gray, gray, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
174
60
 
175
-
61
+
176
62
 
177
63
  std::vector<std::vector<cv::Point>> contours;
178
64
 
@@ -180,25 +66,73 @@
180
66
 
181
67
  cv::findContours(gray, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_TC89_L1);
182
68
 
183
-
69
+
184
70
 
185
71
  int max_level = 0;
186
72
 
187
73
  for(int i = 0; i < contours.size(); i++){
188
74
 
189
- cv::drawContours(mat, contours, i, cv::Scalar(255, 0, 0, 255), 3, CV_AA, hierarchy, max_level);
75
+ cv::drawContours(gray, contours, i, cv::Scalar(255, 0, 0, 255), 3, CV_AA, hierarchy, max_level);
190
76
 
191
77
  }
192
78
 
193
79
 
194
80
 
81
+ max_level = 0;
82
+
83
+
84
+
85
+ for(int i = 0; i < contours.size(); i++) {
86
+
87
+ // ある程度の面積が有るものだけに絞る
88
+
89
+ double a = contourArea(contours[i],false);
90
+
91
+ if(a > 15000) {
92
+
93
+ //輪郭を直線近似する
94
+
95
+ std::vector<cv::Point> approx;
96
+
97
+ cv::approxPolyDP(cv::Mat(contours[i]), approx, 0.01 * cv::arcLength(contours[i], true), true);
98
+
99
+ // 矩形のみ取得
100
+
101
+ if (approx.size() == 4) {
102
+
103
+ cv::drawContours(gray, contours, i, cv::Scalar(255, 0, 0, 255), 3, CV_AA, hierarchy, max_level);
104
+
105
+ }
106
+
107
+ }
108
+
109
+ }
110
+
111
+
112
+
113
+ cv::Point2f src[4]; // 変換元
114
+
115
+ cv::Point2f dst[4]; // 変換先
116
+
117
+ cv::Mat perspective_matrix = cv::getPerspectiveTransform(src, dst);
118
+
119
+ cv::warpPerspective(gray, gray, perspective_matrix, gray.size(), cv::INTER_LINEAR);
120
+
121
+
122
+
195
- return MatToUIImage(mat);
123
+ input_img = MatToUIImage(gray); //---④
124
+
125
+
126
+
127
+ return input_img; //---⑤
196
128
 
197
129
  }
198
130
 
199
131
 
200
132
 
201
133
  @end
134
+
135
+
202
136
 
203
137
  ```
204
138