Introduction to Python OpenCV programming using Windows Visual Studio Code

Tweet


Tutorial of OpenCV programming. Windows Visual Studio Code Python programming. Quick learning of basic knowledge of OpenCV programming.


Install IrfanView


Install Anaconda


Anaconda enviroment

Run following commands at terminal.

pip install --user --upgrade pip
pip install numpy
pip install opencv-python


Install Visual Studio Code


Install Python in Visual Studio Code

Write python at search area of extension. Install Python(Microsoft).


Programming in Visual Studio Code

Select explorer from left icon, and open folder.

Right click explorer and clicke [new file]. Write file name.

Click blue status bar at left bottom. If [Select the workspace to set the interpreter] appear, select working directory you made just now. Choose the environment you made just now at Anaconda when [select interpreter] appeared.

Write as follows at source code edit area.

import numpy
import cv2
print(numpy.__version__)
print(cv2.__version__)

Click [run python file at terminal] at the play button at right top. Result is shown in the bottom [terminal].

Programming matrix calculation

import numpy as np
def main():
    mata=np.zeros((2,2),dtype=np.float64)
    vecx=np.zeros((2,1),dtype=np.float64)
    mata[0,0]=1
    mata[0,1]=2
    mata[1,0]=3
    mata[1,1]=4
    vecx[0,0]=2
    vecx[1,0]=1
    vecb=mata@vecx
    print('a=',mata)
    print('x=',vecx)
    print('b=',vecb)
if __name__=='__main__':
    main()


Programming sketching

import numpy as np
import cv2
def main():
    img=np.zeros((480,640,3),dtype=np.uint8)
    cv2.line(img,(100,100),(200,100),(0,0,255))
    cv2.rectangle(img,(100,200),(200,300),(0,255,0))
    cv2.circle(img,(400,200),50,(255,0,0))
    cv2.imshow('opencv program',img)
    cv2.waitKey()
    cv2.destroyAllWindows()
if __name__=='__main__':
    main()


Basic structure of programming image processing

meal41.jpg

import numpy as np
import cv2
def main():
    input=cv2.imread('meal41.jpg',cv2.IMREAD_COLOR)
    if input is None:
        print('Cannot open file')
        return
    rows,cols,_=input.shape
    output=input.copy()
    for y in range(0,rows):
        for x in range(0,cols):
            r=input[y,x,2]
            g=input[y,x,1]
            b=input[y,x,0]
            output[y,x,2]=0.9*r
            output[y,x,1]=0.7*g
            output[y,x,0]=0.4*b
    cv2.imwrite('output.bmp',output)
if __name__=='__main__':
    main()


Image processing programming using neighboring pixels

grayscale.jpg

import numpy as np
import cv2
def main():
    input=cv2.imread('grayscale.jpg',cv2.IMREAD_GRAYSCALE)
    if input is None:
        print('Cannot open file')
        return
    rows,cols=input.shape
    output=input.copy()
    for y in range(0,rows):
        for x in range(0,cols):
            num=0
            val=0
            for yy in [y-1,y,y+1]:
                for xx in [x-1,x,x+1]:
                    if 0<=xx<cols and 0<=yy<rows:
                        val+=input[yy,xx]
                        num+=1
            output[y,x]=val/num
    cv2.imwrite('output.bmp',output)
if __name__=='__main__':
    main()


Implementation example of photometric stereo

input0.bmp
input1.bmp
input2.bmp

inside.byte
Normal2Height.exe

import numpy as np
import cv2
def main():
    input=[]
    input.append(cv2.imread('input0.bmp',cv2.IMREAD_GRAYSCALE))
    input.append(cv2.imread('input1.bmp',cv2.IMREAD_GRAYSCALE))
    input.append(cv2.imread('input2.bmp',cv2.IMREAD_GRAYSCALE))
    if input[0] is None or input[1] is None or input[2] is None:
        print('Cannot open file')
        return
    rows,cols=input[0].shape
    matl=np.zeros((3,3),dtype=np.float64)
    matl[0,0]=1.58546E-17
    matl[0,1]=0.258819045
    matl[0,2]=0.965925826
    matl[1,0]=-0.353553391
    matl[1,1]=-0.353553391
    matl[1,2]=0.866025404
    matl[2,0]=0.353553391
    matl[2,1]=-0.353553391
    matl[2,2]=0.866025404
    invl=np.linalg.inv(matl)
    output=np.zeros((rows,cols,3),dtype=np.float32)
    for y in range(0,rows):
        for x in range(0,cols):
            veci=np.zeros((3,1),dtype=np.float64)
            veci[0,0]=input[0][y,x]
            veci[1,0]=input[1][y,x]
            veci[2,0]=input[2][y,x]
            vecn=invl@veci
            r=np.linalg.norm(vecn)
            if r>1e-15:
                vecn/=r
            output[y,x,0]=vecn[0,0]
            output[y,x,1]=vecn[1,0]
            output[y,x,2]=vecn[2,0]
    with open('normal.float',mode='wb') as f:
        output.astype(np.float32).tofile(f)
if __name__=='__main__':
    main()


Implementation example of camera calibration

image1.jpg
image2.jpg

import numpy as np
import cv2
def main():
    input=[]
    input.append(cv2.imread('image1.jpg',cv2.IMREAD_COLOR))
    input.append(cv2.imread('image2.jpg',cv2.IMREAD_COLOR))
    if input[0] is None or input[1] is None:
        print('Cannot open file')
        return
    output=[input[0].copy(),input[1].copy()]
    rows,cols,_=input[0].shape
    list3d=[]
    list2d=[]
    item3d=[]
    item2d=[]
    item3d.append([1,0,0])
    item2d.append([234,306])
    item3d.append([0,1,0])
    item2d.append([448,265])
    item3d.append([0,0,1])
    item2d.append([320,90])
    item3d.append([1,1,0])
    item2d.append([391,370])
    item3d.append([0,1,1])
    item2d.append([475,121])
    item3d.append([1,0,1])
    item2d.append([225,147])
    item3d.append([1,1,1])
    item2d.append([413,196])
    list3d.append(item3d)
    list2d.append(item2d)
    item3d=[]
    item2d=[]
    item3d.append([1,0,0])
    item2d.append([174,249])
    item3d.append([0,1,0])
    item2d.append([384,276])
    item3d.append([0,0,1])
    item2d.append([298,69])
    item3d.append([1,1,0])
    item2d.append([244,359])
    item3d.append([0,1,1])
    item2d.append([404,154])
    item3d.append([1,0,1])
    item2d.append([138,125])
    item3d.append([1,1,1])
    item2d.append([226,239])
    list3d.append(item3d)
    list2d.append(item2d)
    list3d=np.array(list3d,dtype=np.float32)
    list2d=np.array(list2d,dtype=np.float32)
    intrinsic=np.array([
        [1000,0,cols/2],
        [0,1000,rows/2],
        [0,0,1]
        ],dtype=np.float64)
    dist=np.zeros((1,4),dtype=np.float64)
    _,intrinsic,dist,vecr,vect=cv2.calibrateCamera(
    list3d,list2d,(cols,rows),intrinsic,dist,
    flags=
    cv2.CALIB_USE_INTRINSIC_GUESS|
    cv2.CALIB_FIX_PRINCIPAL_POINT|
    cv2.CALIB_FIX_ASPECT_RATIO|
    cv2.CALIB_ZERO_TANGENT_DIST|
    cv2.CALIB_FIX_K1|
    cv2.CALIB_FIX_K2|
    cv2.CALIB_FIX_K3|
    cv2.CALIB_FIX_K4|
    cv2.CALIB_FIX_K5|
    cv2.CALIB_FIX_K6)
    print('Camera intrinsic matrix')
    print(intrinsic)
    print('Translation vector')
    print(vect[0])
    print(vect[1])
    print('Rotation matrix')
    matr=[cv2.Rodrigues(vecr[0])[0],cv2.Rodrigues(vecr[1])[0]]
    print(matr[0])
    print(matr[1])
    for c in range(0,2):
        for i in [-1,0,1,2]:
            point=[]
            point.append(np.array([[-1],[i],[0]],dtype=np.float64))
            point.append(np.array([[2],[i],[0]],dtype=np.float64))
            point.append(np.array([[i],[-1],[0]],dtype=np.float64))
            point.append(np.array([[i],[2],[0]],dtype=np.float64))
            point=[intrinsic@(matr[c]@p+vect[c]) for p in point]
            point=[[int(p[0,0]/p[2,0]),int(p[1,0]/p[2,0])] for p in point]
            cv2.line(output[c],point[0],point[1],(0,0,255),
            thickness=3,lineType=cv2.LINE_AA)
            cv2.line(output[c],point[2],point[3],(0,0,255),
            thickness=3,lineType=cv2.LINE_AA)
    cv2.imwrite('output1.jpg',output[0])
    cv2.imwrite('output2.jpg',output[1])
if __name__=='__main__':
    main()


VS Code Extensions

VS Code extensions for Python programming.


Back