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 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