Skip to content

Instantly share code, notes, and snippets.

@Roger8
Last active November 7, 2018 07:59
Show Gist options
  • Save Roger8/266bd516fb667ae56d7949e33a69e8e9 to your computer and use it in GitHub Desktop.
Save Roger8/266bd516fb667ae56d7949e33a69e8e9 to your computer and use it in GitHub Desktop.
【opencv】

opencv 中函数的用法例子**

@Roger8
Copy link
Author

Roger8 commented Jan 29, 2018

adaptiveThreshold, filter2D, Scharr

    adaptThresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 61,0)
    show('x',adaptThresh)

    medflt = cv2.medianBlur(gray, 11)
    dm = cv2.subtract(medflt, gray) 
    dm = cv2.convertScaleAbs(dm)
    bw= cv2.threshold(dm, 0, 255, cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)[1]
    show('x', bw)

    kernel = np.ones((11,11), dtype=np.float32)/121
    flt = cv2.filter2D(gray, -1, kernel)
    show('x', flt)
    dm = cv2.subtract(flt , gray) 
    show('x', dm)

    dx = cv2.Scharr(dm, -1,1,0)
    dy = cv2.Scharr(dm, -1,0,1)
    edge =  cv2.add(dx ,dy)
    edge = cv2.convertScaleAbs(edge)

@Roger8
Copy link
Author

Roger8 commented Jan 29, 2018

Line detect: Hough transform

lines = cv2.HoughLinesP(edges,1,np.pi/180,30,minLineLength=60,maxLineGap=10)
lines1 = lines[:,0,:]#提取为二维
for x1,y1,x2,y2 in lines1[:]: 
    cv2.line(img,(x1,y1),(x2,y2),(255,0,0),1)
lines = cv2.HoughLines(edges,1,np.pi/180,160)
lines1 = lines[:,0,:]#提取为为二维
for rho,theta in lines1[:]: 
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a)) 
    cv2.line(img,(x1,y1),(x2,y2),(255,0,0),1)

@Roger8
Copy link
Author

Roger8 commented Jan 29, 2018

erode, dilate

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
closed = cv2.erode(closed, None, iterations = 4)
closed = cv2.dilate(closed, None, iterations = 4)
  • 腐蚀

在数学形态学运算中的作用是消除边界点,使边界向内部收缩

  • 膨胀

将与物体接触的所有背景点合并到该物体中,使边界向外部扩张

  • 开运算(Opening Operation)

先腐蚀后膨胀的过程:(可以用来消除小物体、在纤细点处分离物体、平滑较大物体的边界的同时并不明显改变其面积)

  • 闭运算(Closing Operation)

先膨胀后腐蚀的过程: ( 能够排除小型黑洞(黑色区域)

  • 形态学梯度(Morphological Gradient)

为膨胀图与腐蚀图之差: 可以将团块(blob)的边缘突出出来, 可以用形态学梯度来保留物体的边缘轮廓

  • 顶帽运算(Top Hat)

为原图像与“开运算“的结果图之差: 因为开运算带来的结果是放大了裂缝或者局部低亮度的区域,因此,从原图中减去开运算后的图,得到的效果图突出了比原图轮廓周围的区域更明亮的区域,且这一操作和选择的核的大小相关 。 往往用来分离比邻近点亮一些的斑块。当一幅图像具有大幅的背景的时候,而微小物品比较有规律的情况下,可以使用顶帽运算进行背景提取。

  • 黑帽(Black Hat)

为”闭运算“的结果图与原图像之差: 突出了比原图轮廓周围的区域更暗的区域,且这一操作和选择的核的大小相关。所以,黑帽运算用来分离比邻近点暗一些的斑块

@Roger8
Copy link
Author

Roger8 commented Feb 1, 2018

  • 【opencv】中getPerspectiveTransform()函数选择初始区域的四个点和结束区域的四个点的顺序必须是:左上、右上、左下、右下
  • java 中Size(width, height) ,分别 对应Mat中的width->Mat.cols() , height->Mat.rows(), 与numpy 中的shape 相反

@Roger8
Copy link
Author

Roger8 commented Feb 5, 2018

java/android 中使用注意

  1. Utils.matToBitmap( mat, bmp) 函数
     * @param mat is a valid input Mat object of types 'CV_8UC1', 'CV_8UC3' or 'CV_8UC4'.
     * @param bmp is a valid Bitmap object of the same size as the Mat and of type 'ARGB_8888' or 'RGB_565'.
     idregionMat.convertTo(idregionMat, CvType.CV_8UC1);
     Utils.matToBitmap(idregionMat , gBitmap);// 

@Roger8
Copy link
Author

Roger8 commented Feb 8, 2018

求文字区域旋转角度后扭正

# grab the (x, y) coordinates of all pixel values that
# are greater than zero, then use these coordinates to
# compute a rotated bounding box that contains all
# coordinates
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]
 
# the `cv2.minAreaRect` function returns values in the
# range [-90, 0); as the rectangle rotates clockwise the
# returned angle trends to 0 -- in this special case we
# need to add 90 degrees to the angle
if angle < -45:
	angle = -(90 + angle)

# otherwise, just take the inverse of the angle to make
# it positive
else:
	angle = -angle

# rotate the image to deskew it
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h),
flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

@Roger8
Copy link
Author

Roger8 commented Sep 3, 2018

写视频

import cv2
import os

#图片路径
im_dir = '/home/suanfa/data/out/201708231503440'
#输出视频路径
video_dir = '/home/suanfa/data/out/201708231503440-1018.avi'
#帧率
fps = 30  
#图片数 
num = 426
#图片尺寸
img_size = (841,1023)

#fourcc = cv2.cv.CV_FOURCC('M','J','P','G')#opencv2.4
fourcc = cv2.VideoWriter_fourcc('M','J','P','G') #opencv3.0
videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)

for i in range(1,num):
    im_name = os.path.join(im_dir, str(i).zfill(6)+'.jpg')
    frame = cv2.imread(im_name)
    videoWriter.write(frame)
    print im_name

videoWriter.release()
print 'finish'

@Roger8
Copy link
Author

Roger8 commented Oct 23, 2018

读视频 获取视频参数:帧数, 宽/高,fps

cap = cv2.VideoCapture('MOT16-07.mp4')
# 帧数
cap.get(cv2.CAP_PROP_FRAME_COUNT)
# fps
cap.get(cv2.CAP_PROP_FPS)
#  以帧为单位的当前位置
cap.get(cv2.CAP_PROP_POS_FRAMES)
# 跳到指定帧
# jump to specific frame
framestart = 150
cap.set(cv2.CAP_PROP_POS_FRAMES, int(framestart))
ret ,frm= cap.read()
# 宽/高
cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
cap.get(cv2.CAP_PROP_FRAME_WIDTH)
# 编码方式
codec = int(cap.get(cv2.CAP_PROP_FOURCC)) # 828601953
chr(codec&0xFF) + chr((codec>>8)&0xFF) + chr((codec>>16)&0xFF) + chr((codec>>24)&0xFF) # 转换为字符 avc1 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment