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

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

OHNO
September 10, 2020

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

OHNO

September 10, 2020
Tweet

More Decks by OHNO

Other Decks in Programming

Transcript

  1. 画像の読み込みと表示 #カラー img1=cv2.imread("lena.jpg",1) cv2.imshow ( "img1", img1 ) cv2.waitKey(1000) #1秒間待機

    #白黒 img1=cv2.imread("lena.jpg",0) cv2.imshow ( "img2", img2 ) cv2.imwrite(“img.png” , img2) #画像の保存 cv2.waitKey(0) #Enterキーで終了 Test0.py
  2. 画像の反転、回転1 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) Test4.py
  3. 画像の回転2 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) Test5.py
  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) Test7.py
  5. フィルタ処理 -1 0 1 -2 0 2 -1 0 1

    一定の大きさのカーネル(オペレータ)で積和演算(畳み 込み計算)を行う。全画素に対してこの計算を実施。
  6. フィルタ処理(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) img1=cv2.imread("building1.png",0) img3=img2|img3 cv2.imshow ( "img3", img3 ) cv2.waitKey(1000) 縦方向の変化を強調 -1 -2 -1 0 0 0 1 2 1 Test11.py
  7. ラベルの色付け 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) Test13.py 数字の場合 OpenCVで1文字ずつ切り出 しディープラーニングで認識
  8. 2値化 img1=cv2.imread("kizu.png",0) cv2.imshow ( "img1", img1 ) #2値化 ret,img2=cv2.threshold(img1,120,255,1) cv2.imshow

    ( "img2", img2 ) cv2.waitKey(1000) ・2値化の閾値を変えてみよう Test14.py
  9. 地の明るさ img1=cv2.imread("kizu.png",0) img2=np.zeros([256,400]).astype('uint8') for x in range(0,400): dat=0 for y

    in range(80,90): dat=dat+img1[y,x] dat=int(0.1*dat) cv2.line(img2,(x,0),(x,dat),(255,25,255)) cv2.imshow ( "img1", img1 ) cv2.imshow ( "img2", img2 ) cv2.waitKey(0) Test15.py 地の明るさが不均一
  10. 動的2値化処理 地の明るさを均一に img1=cv2.imread("kizu.png",0) #平均値フィルタ img2=cv2.blur(img1,(32,32)) img3=img1-img2+127 cv2.imshow ( "img1", img1

    ) cv2.imshow ( "img2", img2 ) cv2.imshow ( "img3", img3 ) cv2.imwrite ( “kizu1.png", img3 ) cv2.waitKey(0) Test16.py
  11. 地の明るさ(改良後) img1=cv2.imread("kizu1.png",0) img2=np.zeros([256,400]).astype('uint8') for x in range(0,400): dat=0 for y

    in range(80,90): dat=dat+img1[y,x] dat=int(0.1*dat) cv2.line(img2,(x,0),(x,dat),(255,25,255)) cv2.imshow ( "img2", img2 ) cv2.waitKey(0) 地の明るさが均一に Test17.py
  12. 動的2値化で不良検出 2/2 n = label[0] - 1 data = np.delete(label[2],

    0, 0) center=np.delete(label[3], 0, 0) for i in range(n): if(data[i][4]>30): str1=“n:”+str(data[i][4])+“ x:”+str(int(center[i][0]))+“ y:"+str(int(center[i][1])) print(str1) x0 = data[i][0]-2 y0 = data[i][1]-2 x1 = data[i][0] + data[i][2]+2 y1 = data[i][1] + data[i][3]+2 cv2.rectangle(color, (x0, y0), (x1, y1), (0, 0, 255)) cv2.imshow ( "color", color )
  13. Pythonプログラム1/3 from pypylon import pylon from pypylon import genicam import

    sys import cv2 import numpy as np width = 2044 height = 1536 img1 = np.zeros((height, width, 1), np.uint8) countOfImagesToGrab = 10 exitCode = 0 try: camera =pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice()) camera.Open() print("Using device ", camera.GetDeviceInfo().GetModelName()) new_width = camera.Width.GetValue() - camera.Width.GetInc()
  14. Pythonプログラム2/3 if new_width >= camera.Width.GetMin(): camera.Width.SetValue(new_width) camera.MaxNumBuffer = 5 while

    True: camera.StartGrabbingMax(1) while camera.IsGrabbing(): grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException) if grabResult.GrabSucceeded(): img = grabResult.Array cv2.imshow("img",img) key=cv2.waitKey(1) else: print("Error: ", grabResult.ErrorCode, grabResult.ErrorDescription) if key=='q’: break grabResult.Release()
  15. Pythonプログラム3/3 camera.Close() except genicam.GenericException as e: # Error handling. print("An

    exception occurred.") print(e.GetDescription()) exitCode = 1 sys.exit(exitCode)