LCDや7SEGに表示される数字を自動で読み取りたいと思って実験してみた。画像処理はほとんどやったことがないのだがググってみると一般的には複数の画像処理を行い最終的には二値化画像を生成するようなのでまねてやってみたら...元画像の品質や外光による影響を大きく受けたりして調整作業がとてもシビアだ。
もっと安直な方法はないものかと...外光の映り込みによる影響を受けること、フルカラー画像をグレイスケール変換してしまうと重要な色情報が欠落してしまうこと、などから輝度差を除外した色差による画像処理方法を考えてみた。
色差は隣接するピクセルのRGB値をそれぞれ減算して求めるが輝度差の影響を排除するため減算結果の最小値/最大値の差を色差とする方法を実験してみた。RGBの場合、輝度が違う同色は各RGB値が一律に変化することに着目した方法である。
【色差の計算方法】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
(100, 100, 100) -> (120, 120, 120) = (+20, +20, +20) ... 色差( 0) (100, 100, 100) -> (120, 110, 100) = (+20, +10, 0) ... 色差(20) (100, 100, 100) -> (120, 90, 80) = (+20, -10, -20) ... 色差(40) (100, 100, 100) -> ( 50, 50, 10) = (-50, -50, -90) ... 色差(40) int diffColor(RGBTRIPLE p1, RGBTRIPLE p2) { int b = (int)p1.rgbtBlue - (int)p2.rgbtBlue; int g = (int)p1.rgbtGreen - (int)p2.rgbtGreen; int r = (int)p1.rgbtRed - (int)p2.rgbtRed; int max = r > g ? (r > b ? r : b) : (g > b ? g : b); int min = r < g ? (r < b ? r : b) : (g < b ? g : b); return max - min; } |
※単純にするため本プログラムでは人間の目の色感度差等の補正はしていないがカメラ側が補正しているなら同じように補正すべきかも。
最初はカラーエッジ検出だけを考えていたが色差が判断できるのならその逆の同色も判断できるだろうと指定ピクセルと同色のピクセルを検出する実験をしてみたところ想定外にうまくいってしまった。しかも数字認識処理のために画像イメージ全体を前処理する必要もなく極めて高速に処理できるというメリットがある。
【参考情報】
色差式というものがあるが今回は使っていない。
新しい色差式(CIE DE2000)について。
17. 色差について
【元画像】
test.bmp (クリック後に元画像のDLが可能)
【カラーエッジ検出】
./icdd test.bmp -e 5
【同色検出】
./icdd test.bmp -f 1950 1140 50
【プログラム (Eclipse-MinGW) 】
目視確認用のモノクロ画像を生成するプログラム。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
/* main.cpp - Image Color Difference Detector Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <stdio.h> #include "bitmap.h" #define OFFSET 1 typedef struct __attribute__((packed)) { int x, y, threshold; } fill_t; typedef struct __attribute__((packed)) { int x, y, width, height, count; } box_t; bool detect(const char *image, const char *save, int edge, fill_t *fill, box_t *box) { Bitmap source, output; int x, y, i; if (!source.read(image)) { printf("file not found: \"%s\"\n", image); return false; } int32_t width = source.width(); int32_t height = source.height(); if (source.bitCount() != 24) { printf("image(\"%s\"): { width=%d, height=%d, bitCount=%u }\n", image, width, height, source.bitCount()); printf("This program is only 24 bit color mode\n"); return false; } width = abs(width); height = abs(height); output.create(width, height); Bitmap::RGBTRIPLE color = source.getPixelColor(fill->x, fill->y); for (y = 0; y < height; ++y) { for (x = OFFSET; x < width; ++x) { Bitmap::RGBTRIPLE c = source.getPixelColor(x, y); if ((edge && (Bitmap::diffColor(source.getPixelColor(x - OFFSET, y), c) >= edge)) || (fill && (Bitmap::diffColor(color, c) < fill->threshold))) output.setPixel(x, y, true); } } for (x = 0; x < width; ++x) { for (y = OFFSET; y < height; ++y) { Bitmap::RGBTRIPLE c = source.getPixelColor(x, y); if ((edge && (Bitmap::diffColor(source.getPixelColor(x, y - OFFSET), c) >= edge)) || (fill && (Bitmap::diffColor(color, c) < fill->threshold))) output.setPixel(x, y, true); } } for (i = 0; i < box->count; ++i) { int off = box->width * i + box->x; for (x = 0; x < box->width; ++x) { output.setPixel(off + x, box->y, true); output.setPixel(off + x, box->y + box->height, true); } for (y = 0; y < box->height; ++y) { output.setPixel(off, box->y + y, true); output.setPixel(off + box->width, box->y + y, true); } } return output.write(save); } void usage(void) { printf("Image Color Difference Detector, version 1.00\n"); printf("Copyright(c) 2023 Sasapea's Lab. All right reserved.\n"); printf("\n"); printf("Usage: icdd [options] image-file.bmp\n"); printf("\n"); printf(" options:\n"); printf(" -o filename ... save file name\n"); printf(" -e n ... edge threshold\n"); printf(" -f x y n ... paint fill\n"); printf(" -b x y w h n ... draw box\n"); printf(" -h ... this help\n"); printf("\n"); exit(1); } int main(int argc, char **argv) { const char *name = nullptr; const char *save = "save.bmp"; int edge = 0; fill_t fill = {}; box_t box = {}; for (int i = 1; i < argc; ) { char *p = argv[i++]; if (*p == '-') { ++p; if (strcmp(p, "o") == 0) { if (i < argc) save = argv[i++]; } else if (strcmp(p, "e") == 0) { if (i < argc) edge = atoi(argv[i++]); } else if (strcmp(p, "f") == 0) { if (i < argc) fill.x = atoi(argv[i++]); if (i < argc) fill.y = atoi(argv[i++]); if (i < argc) fill.threshold = atoi(argv[i++]); } else if (strcmp(p, "b") == 0) { if (i < argc) box.x = atoi(argv[i++]); if (i < argc) box.y = atoi(argv[i++]); if (i < argc) box.width = atoi(argv[i++]); if (i < argc) box.height = atoi(argv[i++]); if (i < argc) box.count = atoi(argv[i++]); } else if (strcmp(p, "h") == 0) { usage(); } } else { name = p; } } if (!name) usage(); return detect(name, save, edge, &fill, &box) ? 0 : 1; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
/* bitmap.h - Bitmap Library Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <sys/stat.h> #define BITMAPFILETYPE "BM" class Bitmap { public: typedef struct __attribute__((packed)) { uint8_t rgbtBlue; uint8_t rgbtGreen; uint8_t rgbtRed; } RGBTRIPLE; typedef struct __attribute__((packed)) { uint8_t rgbBlue; uint8_t rgbGreen; uint8_t rgbRed; uint8_t rgbReserved; } RGBQUAD; const RGBTRIPLE COLOR_BLACK = { 0x00, 0x00, 0x00 }; const RGBTRIPLE COLOR_WHITE = { 0xFF, 0xFF, 0xFF }; Bitmap(void) : _bitmap(nullptr) { clear(); } Bitmap(Bitmap& from) : Bitmap() { copy(from); } Bitmap(int32_t width, int32_t height, uint16_t bitCount) : Bitmap() { create(width, height, bitCount); } ~Bitmap(void) { clear(); } void clear(void) { bitmap(nullptr); } bool copy(Bitmap& from) { BITMAPHEADER *bh = nullptr; if (from._bitmap) { if ((bh = (BITMAPHEADER *)malloc(from._bitmap->bfSize))) { memcpy(bh, from._bitmap, from._bitmap->bfSize); bitmap(bh); } } return bh != nullptr; } int32_t width(void) { return _width; } int32_t height(void) { return _height; } uint32_t bitCount(void) { return _bitCount; } size_t size(void) { return _sizeImage; } int32_t lineBytes(void) { return _lineBytes; } uint8_t *image(void) { return _images; } bool read(const char *file) { bool rv = false; BITMAPHEADER *bh; struct stat stb; FILE *fp; if ((fp = fopen(file, "rb"))) { if (fstat(fileno(fp), &stb) != -1) { if ((size_t)stb.st_size >= sizeof(BITMAPHEADER)) { if ((bh = (BITMAPHEADER *)malloc(stb.st_size))) { if (fread(bh, 1, stb.st_size, fp) != (size_t)stb.st_size) { free(bh); fprintf(stderr, "[Error] [%s] file read error\n", file); } else if (memcmp(&bh->bfType, BITMAPFILETYPE, sizeof(bh->bfType)) != 0) { free(bh); fprintf(stderr, "[Error] [%s] invalid BITMAPFILEHEADER.bfType\n", file); } else if (bh->bfOffBits < sizeof(BITMAPHEADER) + sizeof(RGBQUAD) * bh->biClrUsed) { free(bh); fprintf(stderr, "[Error] [%s] invalid BITMAPFILEHEADER.bfOffBits\n", file); } else if ((bh->biCompression == 0) && ((bh->bfOffBits + abs(lineBytes(bh) * bh->biHeight) > (size_t)stb.st_size))) { free(bh); fprintf(stderr, "[Error] [%s] invalid BITMAPFILEHEADER.bfOffBits\n", file); } else { bh->bfSize = stb.st_size; bh->biPlanes = 1; if (bh->biCompression == 0) bh->biSizeImage = abs(lineBytes(bh) * bh->biHeight); bitmap(bh); rv = true; } } } } fclose(fp); } return rv; } bool write(const char *file) { bool rv = false; FILE *fp; if (_bitmap) { if ((fp = fopen(file, "wb"))) { rv = fwrite(_bitmap, 1, _bitmap->bfSize, fp) == _bitmap->bfSize; fclose(fp); } } return rv; } void create(int32_t width, int32_t height, uint16_t bitCount = 1) { BITMAPHEADER *bh; if (bitCount == 0) bitCount = 1; size_t hs = sizeof(BITMAPHEADER) + (bitCount > 8 ? 0 : sizeof(RGBQUAD) * (1 << bitCount)); size_t is = abs(lineBytes(width, height, bitCount) * height); size_t fs = hs + is; if ((bh = (BITMAPHEADER *)calloc(fs, 1))) { memcpy(&bh->bfType, BITMAPFILETYPE, sizeof(bh->bfType)); bh->bfSize = fs; bh->bfOffBits = hs; bh->biSize = sizeof(BITMAPHEADER) - offsetof(BITMAPHEADER, biSize); bh->biWidth = width; bh->biHeight = height; bh->biPlanes = 1; bh->biBitCount = bitCount; bh->biSizeImage = is; bh->biClrUsed = bitCount > 8 ? 0 : 1 << bitCount; bitmap(bh); if (bh->biClrUsed) { for (uint32_t i = 0; i < bh->biClrUsed - 1; ++i) setColorPallte(i, i, i, i); setColorPallte(bh->biClrUsed - 1, 0xFF, 0xFF, 0xFF); } } } void setColorPallte(uint8_t index, uint8_t blue, uint8_t green, uint8_t red) { RGBQUAD rgb = { .rgbBlue = blue, .rgbGreen = green, .rgbRed = red }; setColorPallte(index, rgb); } void setColorPallte(uint8_t index, RGBTRIPLE rgbt) { RGBQUAD rgb = { .rgbBlue = rgbt.rgbtBlue, .rgbGreen = rgbt.rgbtGreen, .rgbRed = rgbt.rgbtRed }; setColorPallte(index, rgb); } void setColorPallte(uint8_t index, RGBQUAD rgb) { if (_bitmap) { if (index < _bitmap->biClrUsed) _bitmap->biColors[index] = rgb; } } void setPixel(int x, int y, bool on) { if (confirm(x, y, 1)) { uint8_t *img = &_images[(_lineBytes * y) + (x >> 3)]; uint8_t bit = 1 << (7 - (x & 7)); if (on) *img |= bit; else *img &= ~bit; } } bool getPixel(int x, int y) { return confirm(x, y, 1) && (_images[_lineBytes * y + (x >> 3)] & (1 << (7 - (x & 7)))); } void setPixelColorIndex(int x, int y, uint8_t index) { if (confirm(x, y, 8) && (index < _bitmap->biClrUsed)) _images[_lineBytes * y + x] = index; } uint8_t getPixelColorIndex(int x, int y) { return confirm(x, y, 8) ? _images[_lineBytes * y + x] : 0; } void setPixelColor(int x, int y, RGBTRIPLE color) { if (confirm(x, y, 24)) *((RGBTRIPLE *)(_images + _lineBytes * y) + x) = color; } RGBTRIPLE getPixelColor(int x, int y) { return confirm(x, y, 24) ? *((RGBTRIPLE *)(_images + _lineBytes * y) + x) : COLOR_BLACK; } static int diffColor(RGBTRIPLE p1, RGBTRIPLE p2) { int b = (int)p1.rgbtBlue - (int)p2.rgbtBlue; int g = (int)p1.rgbtGreen - (int)p2.rgbtGreen; int r = (int)p1.rgbtRed - (int)p2.rgbtRed; int max = r > g ? (r > b ? r : b) : (g > b ? g : b); int min = r < g ? (r < b ? r : b) : (g < b ? g : b); return max - min; } protected: typedef struct __attribute__((packed)) { // // BITMAPFILEHEADER // uint16_t bfType; uint32_t bfSize; uint16_t bfReserved1; uint16_t bfReserved2; uint32_t bfOffBits; // // BITMAPINFOHEADER // uint32_t biSize; int32_t biWidth; int32_t biHeight; uint16_t biPlanes; uint16_t biBitCount; uint32_t biCompression; uint32_t biSizeImage; int32_t biXPelsPerMeter; int32_t biYPelsPerMeter; uint32_t biClrUsed; uint32_t biClrImportant; RGBQUAD biColors[0]; } BITMAPHEADER; BITMAPHEADER *_bitmap; int32_t _width; int32_t _height; uint16_t _bitCount; size_t _sizeImage; int _lineBytes; uint8_t *_images; void bitmap(BITMAPHEADER *bh) { if (_bitmap) free(_bitmap); _bitmap = bh; if (bh) { _width = bh->biWidth; _height = bh->biHeight; _bitCount = bh->biBitCount; _sizeImage = bh->biSizeImage; _lineBytes = lineBytes(bh); _images = (uint8_t *)bh + bh->bfOffBits - (bh->biHeight > 0 ? (bh->biHeight - 1) * _lineBytes : 0); } else { _width = 0; _height = 0; _bitCount = 0; _sizeImage = 0; _lineBytes = 0; _images = nullptr; } } inline int32_t lineBytes(int32_t width, int32_t height, uint16_t bitCount) { int32_t bytes = ((abs(width) * (bitCount ? bitCount : 1) + 31) & ~31) >> 3; return height > 0 ? - bytes : bytes; } inline int32_t lineBytes(BITMAPHEADER *bh) { return lineBytes(bh->biWidth, bh->biHeight, bh->biBitCount); } bool confirm(int x, int y, uint16_t bitCount) { return (x >= 0) && (x < abs(_width)) && (y >= 0) && (y < abs(_height)) && (bitCount == _bitCount); } }; |