Archive

Posts Tagged ‘OpenCV’

[Python]使用OpenCV实现伪彩色和热力图

July 15th, 2019 No comments

使用applyColorMap可以对单个通道的图像进行伪彩色处理和热力图
OpenCV的定义了12种colormap常数,选择一个需要的即可
cv2.applyColorMap(heatmap_g, cv2.COLORMAP_JET)
图像可以使用addWeighted进行叠加处理
cv2.addWeighted(heatmap_img, alpha, merge_img, 1-alpha, 0, merge_img) # 将热度图覆盖到原图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def heatmap_overlay(image,heatmap):
    # 灰度化heatmap
    heatmap_g = heatmap.astype(np.uint8)
    # 热力图伪彩色
    heatmap_color = cv2.applyColorMap(heatmap_g, cv2.COLORMAP_JET)
    # overlay热力图
    merge_img = image.copy()
    heatmap_img = heatmap_color.copy()
    overlay = image.copy()
    alpha = 0.25 # 设置覆盖图片的透明度
    #cv2.rectangle(overlay, (0, 0), (merge_img.shape[1], merge_img.shape[0]), (0, 0, 0), -1) # 设置蓝色为热度图基本色
    cv2.addWeighted(overlay, alpha, merge_img, 1-alpha, 0, merge_img) # 将背景热度图覆盖到原图
    cv2.addWeighted(heatmap_img, alpha, merge_img, 1-alpha, 0, merge_img) # 将热度图覆盖到原图
    return merge_img

参考:
https://blog.csdn.net/u013381011/article/details/78341861

Categories: 语言编程 Tags: ,

[Python]使用OpenCV实现图像和视频转换操作

July 15th, 2019 No comments

将视频按FPS拆解成单张图片
使用cv2.VideoCapture
cv2.VideoCapture(video_path)
计算FPS使用,注意部分压缩视频FPS存在丢帧情况,需要进行跳帧处理
fps = int(vidcap.get(cv2.CAP_PROP_FPS))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def video_split():
  video_path = 'test/video/video_01.mp4'
  video_name = video_path[:-4]
  vidcap = cv2.VideoCapture(video_path)
  success,image = vidcap.read()
  fps = int(vidcap.get(cv2.CAP_PROP_FPS))
  count = 0
  while success:
    image = image_process(image)
    cv2.imwrite("%s/%d.jpg" % (video_name, count), image)
    #if count % fps == 0:
    #    cv2.imwrite("%s/%d.jpg" % (video_name, int(count / fps)), image)
    print('Process %dth seconds: ' % int(count / fps), success)
    success,image = vidcap.read()
    count += 1

Read more…

Categories: 语言编程 Tags: ,

[Python]使用OpenCV进行轮廓检索

July 15th, 2019 No comments

对遮罩层进行轮廓检索并合并到图像上
第一步使用高斯模糊GaussianBlur模糊边缘像素
第二步使用Canny侦测边界,丢弃部分散点
最后使用findContours找到外框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#对遮罩层进行轮廓检索并合并到图像上
def drawMaskContoursOverImage(image,mask):
    # convert colorspace
    gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    #image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
 
    # 3*3 GaussianBlur
    # gray = cv2.GaussianBlur(gray, (3, 3), 0)
    # canny detect edge
    gray = cv2.Canny(gray, 100, 300)
 
    ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
 
    # binary是最后返回的二值图像
    #findContours()第一个参数是源图像、第二个参数是轮廓检索模式,第三个参数是轮廓逼近方法
    #输出是轮廓和层次结构,轮廓是图像中所有轮廓的python列表,每个单独的轮廓是对象边界点的(x,y)坐标的Numpy数组
    binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(image, contours, -1, (0, 255, 0), 1)
 
    # 写图像
    cv2.imwrite('%s.masked.png'%pair[0],image,[int(cv2.IMWRITE_PNG_COMPRESSION),3])

参考
https://blog.csdn.net/dz4543/article/details/80655067

Categories: 语言编程 Tags: ,