###実現内容
c言語、OpenCVを用いて田村の細線化アルゴリズムを作りたいです。画像の1/3しか細線化されていない
参考にしたサイト(http://imagingsolution.blog.fc2.com/blog-entry-138.html)
発生している問題・エラーメッセージ
画像中の幅1/3程度のみ細線化が行われている
該当のソースコード
jp
1#include <iostream> 2#include <opencv2/opencv.hpp> 3#include <opencv2/highgui/highgui.hpp> 4#include<opencv2/imgproc/imgproc.hpp> 5#include<opencv2/core/core.hpp> 6#define rows 1008 7#define cols 981 8 9using namespace std; 10using namespace cv; 11 12int main(int argc, char const *argv[]) { 13 Mat img = imread("C:\aaa.png"); 14 15 int Num = 0; 16 int x, y; 17 int z=0; 18 19 unsigned char Narray[rows][cols] = { 0 }; 20 unsigned char Marray[rows][cols] = { 0 }; 21 22 while (1) { 23 for (y = 1; y < rows - 1; y++) 24 { 25 for (x = 1; x < cols - 1; x++) 26 { 27 while (1) { 28 //pat1 29 //除去する画素 30 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y - 1, x) == 0) 31 Narray[y][x]++; 32 else if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y, x + 1) == 0) 33 Narray[y][x]++; 34 else 35 break; 36 //除去しようとした中で除去しないパターン(not common) 37 if (Narray[y][x] != 0) { 38 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y - 1, x) == 0 && img.at<unsigned char>(y, x + 1) == 255 && img.at<unsigned char>(y + 1, x) == 255 && img.at<unsigned char>(y + 1, x + 1) == 0) 39 { 40 Narray[y][x] = 0; break; 41 } 42 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y - 1, x - 1) == 0 && img.at<unsigned char>(y - 1, x) == 255 && img.at<unsigned char>(y, x - 1) == 255 && img.at<unsigned char>(y, x + 1) == 0) 43 { 44 Narray[y][x] = 0; break; 45 } 46 //除去しないパターン(common) 47 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y, x - 1) == 0 && img.at<unsigned char>(y, x + 1) == 0 && img.at<unsigned char>(y + 1, x) == 255) 48 { 49 Narray[y][x] = 0; break; 50 } 51 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y, x - 1) == 255 && img.at<unsigned char>(y - 1, x) == 0 && img.at<unsigned char>(y + 1, x) == 0) 52 { 53 Narray[y][x] = 0; break; 54 } 55 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y, x - 1) == 0 && img.at<unsigned char>(y, x + 1) == 0 && img.at<unsigned char>(y - 1, x) == 255) 56 { 57 Narray[y][x] = 0; break; 58 } 59 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y - 1, x) == 0 && img.at<unsigned char>(y + 1, x) == 0 && img.at<unsigned char>(y, x + 1) == 255) 60 { 61 Narray[y][x] = 0; break; 62 } 63 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y, x - 1) == 0 && img.at<unsigned char>(y + 1, x) == 0 && img.at<unsigned char>(y + 1, x - 1) == 255) 64 { 65 Narray[y][x] = 0; break; 66 } 67 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y, x - 1) == 0 && img.at<unsigned char>(y - 1, x) == 0 && img.at<unsigned char>(y - 1, x - 1) == 255) 68 { 69 Narray[y][x] = 0; break; 70 } 71 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y - 1, x) == 0 && img.at<unsigned char>(y, x + 1) == 0 && img.at<unsigned char>(y - 1, x + 1) == 255) 72 { 73 Narray[y][x] = 0; break; 74 } 75 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y + 1, x) == 0 && img.at<unsigned char>(y, x + 1) == 0 && img.at<unsigned char>(y + 1, x + 1) == 255) 76 { 77 Narray[y][x] = 0; break; 78 } 79 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y, x + 1) == 255 && img.at<unsigned char>(y, x - 1) == 255 && img.at<unsigned char>(y - 1, x) == 255 && img.at<unsigned char>(y - 1, x - 1) == 0 && img.at<unsigned char>(y - 1, x + 1) == 0 && img.at<unsigned char>(y + 1, x + 1) == 0 && img.at<unsigned char>(y + 1, x - 1) == 0) 80 { 81 Narray[y][x] = 0; break; 82 } 83 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y + 1, x) == 255 && img.at<unsigned char>(y, x + 1) == 255 && img.at<unsigned char>(y - 1, x) == 255 && img.at<unsigned char>(y - 1, x - 1) == 0 && img.at<unsigned char>(y - 1, x + 1) == 0 && img.at<unsigned char>(y + 1, x + 1) == 0 && img.at<unsigned char>(y + 1, x - 1) == 0) 84 { 85 Narray[y][x] = 0; break; 86 } 87 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y + 1, x) == 255 && img.at<unsigned char>(y, x + 1) == 255 && img.at<unsigned char>(y, x - 1) == 255 && img.at<unsigned char>(y - 1, x - 1) == 0 && img.at<unsigned char>(y - 1, x + 1) == 0 && img.at<unsigned char>(y + 1, x + 1) == 0 && img.at<unsigned char>(y + 1, x - 1) == 0) 88 { 89 Narray[y][x] = 0; break; 90 } 91 if (img.at<unsigned char>(y, x) == 255 && img.at<unsigned char>(y + 1, x) == 255 && img.at<unsigned char>(y, x - 1) == 255 && img.at<unsigned char>(y - 1, x) == 255 && img.at<unsigned char>(y - 1, x - 1) == 0 && img.at<unsigned char>(y - 1, x + 1) == 0 && img.at<unsigned char>(y + 1, x + 1) == 0 && img.at<unsigned char>(y + 1, x - 1) == 0) 92 { 93 Narray[y][x] = 0; break; 94 } 95 else break; 96 97 98 } 99 100 } 101 102 103 } 104 } 105 //もし除去する画素(Narrayに1がたっている)があったならNumに0以外を立てるようにする 106 for (y = 1; y < rows - 1; y++) 107 { 108 for (x = 1; x < cols - 1; x++) 109 { 110 111 if (Narray[y][x] !=0) 112 { 113 Num++; 114 } 115 } 116 } 117 printf("%d\n", Num); 118 //Numが0以外ならNaarayに従って画素値を変える、Num=0ということは変える画素値がないため無限ルーぷから抜け出す 119 if (Num != 0) 120 { 121 for (y = 1; y < rows - 1; y++) 122 { 123 for (x = 1; x < cols - 1; x++) 124 { 125 if (Narray[y][x] != 0) 126 { 127 img.at<unsigned char>(y, x) = 0; 128 129 } 130 } 131 } 132 } 133 else//無限ループ抜け出し 134 break; 135 //初期化 136 Narray[rows][cols] = {}; 137 Num = 0; 138 139 for (y = 1; y < rows - 1; y++) 140 { 141 for (x = 1; x < cols - 1; x++) 142 { 143 while (1) { 144 //pat2 145 //pat1とやることは同じ,ただし内容は少し違う,文字数制限のため省略 146 } 147 } 148 } 149 //もし除去する画素(Narrayに1がたっている)があったならNumに0以外を立てるようにする 150 for (y = 1; y < rows - 1; y++) 151 { 152 for (x = 1; x < cols - 1; x++) 153 { 154 155 if (Narray[y][x] != 0) 156 { 157 Num++; 158 } 159 } 160 } 161 //Numが0以外ならNaarayに従って画素値を変える、Num=0ということは変える画素値がないため無限ルーぷから抜け出す 162 if (Num != 0) 163 { 164 for (y = 1; y < rows - 1; y++) 165 { 166 for (x = 1; x < cols - 1; x++) 167 { 168 if (Narray[y][x] != 0) 169 { 170 img.at<unsigned char>(y, x) = 0; 171 172 } 173 } 174 } 175 } 176 else//無限ループ抜け出し 177 break; 178 //初期化 179 Narray[rows][cols] = {}; 180 Num = 0; 181 182 183 184 185 186 } 187 imshow("aaa", img); 188 imwrite("warota.png", img); 189 waitKey(0); 190 return 0;
試したこと
無限ループが終わらず画像が表示されなかったため、Numが0以外でも抜け出せるようにするために、500回回したところで終わるようにしたところ幅1/3のところまでで細線化がおわっている画像が出力されていた。
パターン1,2のどちらにおいてもx,yは走査されていた。(printfで確認)
補足情報(FW/ツールのバージョンなど)
Visual studio2015 OpenCV2.4.11 画像の大きさ(981x1008)
回答2件
あなたの回答
tips
プレビュー