Upgrade to Pro — share decks privately, control downloads, hide ads and more …

画像処理ライブラリOpenCVの使い方

OHNO
June 20, 2020

 画像処理ライブラリOpenCVの使い方

OHNO

June 20, 2020
Tweet

More Decks by OHNO

Other Decks in Research

Transcript

  1. 画像の読み込みと表示 #カラー img1=cv2.imread("lena.jpg",1) #白黒 img1=cv2.imread("lena.jpg",0) #表示 cv2.imshow ( "img1", img1

    ) #表示時間 1秒 cv2.waitKey(1000) #保存 cv2.imwrite("img.png" , img1) USBカメラの画像も 簡単に表示できる
  2. 画像の反転、回転 img1=cv2.imread("building.jpg",1) img2=cv2.flip(img1,0) #上下反転 0 cv2.imshow ( "img2", img2 )

    cv2.waitKey(1000) img2=cv2.flip(img1,1) #左右反転 >0 img2=cv2.flip(img1,-1) #左右反転 <0 img2=cv2.rotate(img1, cv2.ROTATE_90_CLOCKWISE) img2=cv2.rotate(img1, cv2.ROTATE_180) img2=cv2.rotate(img1,cv2.ROTATE_90_COUNTERCLOCKWISE)
  3. 画像の回転 img1=cv2.imread("building.jpg",1) height = img1.shape[0] width = img1.shape[1] center =

    (int(width/2), int(height/2)) angle = 45.0 scale = 1.0 trans = cv2.getRotationMatrix2D(center, angle , scale) img2 = cv2.warpAffine(img1, trans, (width,height)) cv2.imshow ( "img2", img2 ) cv2.waitKey(1000)
  4. 四角形、線、円の描画 img1=cv2.imread("building.jpg",1) cv2.rectangle(img1, (200, 200), (500, 500), (255, 0, 0),

    thickness=2, lineType=cv2.LINE_4) cv2.line(img1, (100, 100), (600, 100), (0, 255, 0), thickness=2, lineType=cv2.LINE_4) cv2.circle(img1, (440,400), 100, (0,0,255), thickness=2, lineType=cv2.LINE_4, shift=0) cv2.imshow ( "img", img1 ) cv2.waitKey(1000)
  5. フィルタ処理(Sobel) img1=cv2.imread("building.jpg",0) img2=cv2.Sobel(img1,cv2.CV_32F,0,1) img2=cv2.convertScaleAbs(img2) cv2.imshow ( "img1", img1 ) cv2.imshow

    ( "img2", img2 ) cv2.waitKey(1000) img3=img2|img3 cv2.imshow ( "img3", img3 ) cv2.waitKey(1000) 縦方向に強調→横線検出 -1 -2 -1 0 0 0 1 2 1
  6. ラベルの色付け colors=[] colors.append([0,0,0]) colors.append([255,0,0]) colors.append([0,255,0]) colors.append([0,0,255]) colors.append([0,255,255]) for y in

    range(0,height): for x in range(0,width): if 0<label[y,x]: img1[y,x]=colors[label[y,x]] else: img1[y,x]=[0,0,0] cv2.imshow ( "img1", img1 ) cv2.waitKey(0)