Python uses OpenCV NumPy to do basic operations of image processing

When doing some image processing or algorithm experiments, it is more convenient to use Python. This article mainly introduces the basic operations of image processing using OpenCV NumPy in Python. Once you are familiar with these basic operations, it will be more convenient to further perform complex operations on images.

like:

  • read pictures
  • save Picture
  • display image
  • RGB channel separation
  • convert to grayscale
  • Take the gray mean
  • Make a mask on the picture or select a specified area
  • Fusion of two pictures
# -*- coding: utf-8 -*-
# @Time    : 2021-04-28 19:45
# @Author  : AlanWang4523
# @FileName: py_pic_handle.py

import os
import sys
import cv2
import numpy as np


def test_handle_img():
    path1 = "niguang_001.jpeg"
    path2 = "niguang_002.jpeg"

    # 根据路径读取图片
    img1 = cv2.imread(path1)
    img2 = cv2.imread(path1)

    # 分离 RGB 三个通道,注意:openCV 中图像格式是 BGR
    srcR = img1[:, :, 2]
    srcG = img1[:, :, 1]
    srcB = img1[:, :, 0]

    # 将原图转成灰度图
    grayImg = 0.299 * srcR + 0.587 * srcG + 0.114 * srcB

    # 获取灰度均值
    meanValueOfGray = np.mean(grayImg)

    # 标记灰度图中大于灰度均值的位置
    mask = grayImg > meanValueOfGray

    # 图像数据类型的转换
    img1 = img1.astype(np.float)
    # img1 = img1.astype(np.uint8)

    # 在原图中抠出灰度大于均值的区域,小于均值的局域置黑(也可以对抠出的区域做处理, 下面代码后面的 * 1 就是模拟要对抠出的区域做处理)
    img1[:, :, 2][mask] = img1[:, :, 2][mask] * 1.0
    img1[:, :, 1][mask] = img1[:, :, 1][mask] * 1.0
    img1[:, :, 0][mask] = img1[:, :, 0][mask] * 1.0
    img1[:, :, 2][~mask] = 0
    img1[:, :, 1][~mask] = 0
    img1[:, :, 0][~mask] = 0

    # 另外一种方式抠图,跟上面效果一样
    img1[:, :, 2] = np.where(grayImg > meanValueOfGray, img1[:, :, 2], 0)
    img1[:, :, 1] = np.where(grayImg > meanValueOfGray, img1[:, :, 1], 0)
    img1[:, :, 0] = np.where(grayImg > meanValueOfGray, img1[:, :, 0], 0)

    # 图像融合
    alpha = 0.3
    img3 = img1 * (1 - alpha) + img1 * alpha

    img1 = img1.astype(np.uint8)

    # 保存图片
    cv2.imwrite('py_test_out_01.png', img1)

    return img1


def hanlde_img(path):
    # 根据路径读取图片
    img = cv2.imread(path)

    img = test_handle_img()

    # 创建图片显示窗口
    title = "ShadowHighlight"
    cv2.namedWindow(title, cv2.WINDOW_NORMAL)   
    cv2.resizeWindow(title, 800, 600)
    cv2.moveWindow(title, 0, 0)
    while True:
        # TODO 如果要看处理后的时候效果,可以在这里处理图片,下面循环显示

        # 循环显示图片,按 ‘q’ 键退出
        cv2.imshow(title, img)
        if cv2.waitKey(1) == ord('q'):
            break
    cv2.destroyAllWindows() 


if __name__ == '__main__':
    '''
        运行环境:Python 3
        执行:python3 py_pic_handle.py <图片路径>
        如:python3 py_pic_handle.py test.jpg
    '''
    if len(sys.argv) == 1:
        print("参数错误:未传入图片路径!")
        sys.exit(-1)
    img_path = sys.argv[1]
    print("img_path Params:", img_path)
    hanlde_img(img_path)

As shown in the figure below: Cut out the area whose gray value is greater than the average gray value from the original image, and set the other areas to black
picture name

Guess you like

Origin blog.csdn.net/u011520181/article/details/116243204