OpenCV 教材 - eiichiromomma/CVMLAB GitHub Wiki

(OpenCV) 教材

しきい値処理用の素材作り

作成した画像

各濃度でその値を記載した画像。 cvThresholdの練習用。

グレー

カラー

ソース

結構適当

Python

#グレースケール
import numpy as np
import cv2
xpos = 10 + np.linspace(0, 560, 16, dtype=np.uint16)
ypos = 20 + np.linspace(0, 320, 16, dtype=np.uint16)
src = np.zeros((350, 610), dtype=np.uint8)
for r in range(16):
    for c in range(16):
        src = cv2.putText(src, str(c+r*16), (xpos[c], ypos[r]), cv2.FONT_HERSHEY_PLAIN,
                          1, c+r*16, 1, cv2.LINE_8)
cv2.imshow('test', src)
cv2.waitKey(0)
cv2.imwrite('numImg.png', src)

カラー

#カラー
import numpy as np
import cv2
xpos = 10 + np.linspace(0, 560, 16, dtype=np.uint16)
ypos = 20 + np.linspace(0, 320, 16, dtype=np.uint16)
src = np.zeros((350*2, 610*2,3), dtype=np.uint8)
for r in range(16):
    for c in range(16):
        src = cv2.putText(src, str(c+r*16), (xpos[c], ypos[r]),
                          cv2.FONT_HERSHEY_PLAIN,1, (c+r*16,c+r*16,c+r*16), 1, cv2.LINE_8)
for r in range(16):
    for c in range(16):
        src = cv2.putText(src, str(c+r*16), (610+xpos[c], ypos[r]),
                          cv2.FONT_HERSHEY_PLAIN,1, (c+r*16,0,0), 1, cv2.LINE_8)

for r in range(16):
    for c in range(16):
        src = cv2.putText(src, str(c+r*16), (xpos[c], 350+ypos[r]),
                          cv2.FONT_HERSHEY_PLAIN,1, (0,c+r*16,0), 1, cv2.LINE_8)

for r in range(16):
    for c in range(16):
        src = cv2.putText(src, str(c+r*16), (610+xpos[c], 350+ypos[r]),
                          cv2.FONT_HERSHEY_PLAIN,1, (0,0,c+r*16), 1, cv2.LINE_8)

cv2.imshow('test', src)
cv2.waitKey(0)
cv2.imwrite('numImg_c.png', src)

C(旧)

    #include <cv.h>
    #include <highgui.h>
    #include <stdlib.h>

    enum{
      IM_HEIGHT=330,
      IM_WIDTH=580,
      CHR_STEP_X=35,
      CHR_STEP_Y=20,
      ORG_X=10,
      ORG_Y=20
    };
    int main(void)
    {
    #if 0 //1:グレー、0:カラー
      IplImage *src = cvCreateImage(cvSize(IM_WIDTH, IM_HEIGHT),IPL_DEPTH_8U, 1);
      cvSetZero(src);
      CvFont font;
      char c_num[4];
      int i;
      cvInitFont(&font, 1, 1.0, 1.0);
      for (i=0; i<256; i++){
        _itoa_s(i,c_num,4,10);
        cvPutText(src,c_num,cvPoint(ORG_X+(i%16)*CHR_STEP_X, ORG_Y+((i)/16)*CHR_STEP_Y),&font, cvScalar(i));
      }
      cvNamedWindow("src",CV_WINDOW_AUTOSIZE);
      cvShowImage("src",src);
      cvWaitKey(0);
      cvSaveImage("numImg.png)",src);
      return 0;
    #else
      IplImage *src = cvCreateImage(cvSize(IM_WIDTH*2, IM_HEIGHT*2),IPL_DEPTH_8U, 3);
      cvSetZero(src);
      CvFont font;
      char c_num[4];
      int i,c;
      cvInitFont(&font, 1, 1.0, 1.0);
      for (c=0; c<4; c++){
        for (i=0; i<256; i++){
          _itoa_s(i,c_num,4,10);
          cvPutText(src,c_num,cvPoint(ORG_X+(i%16)*CHR_STEP_X+IM_WIDTH*(c%2), ORG_Y+((i)/16)*CHR_STEP_Y+IM_HEIGHT*(c/2)),
            &font, cvScalar((c==0||c==3)?i:0,(c==1||c==3)?i:0,(c==2||c==3)?i:0));
        }
      }
      cvNamedWindow("src",CV_WINDOW_AUTOSIZE);
      cvShowImage("src",src);
      cvWaitKey(0);
      cvSaveImage("numImg_c.png)",src);
      return 0;
    #endif
    }
⚠️ **GitHub.com Fallback** ⚠️