首页 > 其他分享 >图像处理

图像处理

时间:2023-02-26 10:13:19浏览次数:39  
标签:plt img 127 cv2 图像处理 灰度 图像

图像处理(Image Process)对输入的图像作某种变换,输出仍然是图像,不涉及或者很少涉及图像内容的分析。例如:图像增强、图像去噪、图像压缩、图像恢复、二值图像处理,以及基于阈值的图像分割等等。

图像分析:对图像内容进行分析,提取有意义的特征,以便于后续的处理。

计算机视觉(CV, Computer Vision):对图像分析得到的特征进行分析,提取场景的语义表示,让计算机具有人眼和人脑的能力。

图像的读取、色彩空间和灰度

import cv2
import numpy as np
import matplotlib.pyplot as plt
def img_show():
    # 原图
    img = cv2.imread("img-input/station-320-180.png", cv2.IMREAD_COLOR)
    img_source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # 灰度图
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_gray_source = cv2.cvtColor(img_gray, cv2.COLOR_BGR2RGB)
    images = [img, img_source, img_gray, img_gray_source]
    for i in range(4):
        plt.subplot(2, 2, i+1)
        plt.imshow(images[i])
    plt.savefig("img-output/colorspace_gray.png")
    plt.show()
if __name__ == '__main__':
    img_show()

result:第一行是原图,第二行是灰度图,第一列是BGR色彩空间,第二列是RGB色彩空间。可见,要得到灰度图,要进行两次转换。

除此之外,也可以直接读取图片为灰度图,但由于默认BGR colorspace,仍然需要进行一次转换,转换为RGB色彩空间:

    img = cv2.imread("img-input/station-320-180.png", 0)
    rows, cols = img.shape
    source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    images = [img, source]
    for i in range(2):
        plt.subplot(2, 1, i+1)
        plt.imshow(images[i])
    plt.show()

result:两张二维图像,没有色彩通道这一维度,1. 默认的BGR,转换为RGB便是常见的灰度图。

阈值Image Thresholding

 简单的阈值操作是,For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise it is set to a maximum value.

二值化(binarization)或阈值化(threshold):旨在提取图像中的目标物体,将背景以及噪声区分开来。通常会设定一个阈值T,通过T将图像的像素划分为两类:大于T的像素群和小于T的像素群。

案例如下,需要注意的是,以下案例是在RGB空间的灰度图上进行thresholding操作的。

def thresholding_apply():
    img = cv2.imread("img-input/station-320-180.png", 0)
    rows, cols = img.shape
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
    ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
    ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
    ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
    images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
    titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
    for i in range(6):
        plt.subplot(3, 2, i+1)
        plt.title(titles[i])
        plt.xticks([])
        plt.yticks([])
        plt.imshow(images[i])
    plt.savefig("img-output/thresholding_rgb.png")
    plt.show()
if __name__ == '__main__':
    thresholding_apply()

result:原图为灰度图,binary是把像素值小于127的置为0,大于127置为1,binary_inv则相反。tozero是大于127的不变,小于127置为0,tozero_inv则相反。trunc则是小于127不变,大于127渐变。

稍微复杂的阈值操作暂略。

图像的形态学操作(Morphological operations):Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images. It needs two inputs, one is our original image, second one is called structuring element or kernel which decides the nature of operation.

参考:https://docs.opencv.org/4.x/d9/d61/tutorial_py_morphological_ops.html

腐蚀和膨胀(Erosion and Dilate)

梯度算子(Morphological Gradient)

开运算和闭运算(Opening and Closing)

顶帽运算和黑帽运算(Top Hat and Black Hat)

标签:plt,img,127,cv2,图像处理,灰度,图像
From: https://www.cnblogs.com/zhaoke271828/p/17145236.html

相关文章

  • 【Python】【图像处理】图片压缩方法
    一直想找个“无损压缩”的办法。当然这并非真的无损。我采用了Opencv的处理图像相关的方法。请见代码:注意:opencv安装:pipinstallopencv-python别想不开installcv2......
  • 【图像处理】-形态学调整(膨胀、腐蚀、开/闭运算)
    形态学调整膨胀操作主要针对卷积计算区域内的该像素点的NxM区域的最大值进行膨胀操作;例如:前景是白色背景经过膨胀后区域会增大原图片(背景为黑色,前景为白色)如下:注:图......
  • 基于OpenMV的图像处理基础知识及例程使用方法
                ......
  • 图像处理之拼接---图像拼接opencv
    基于SURF特征的图像与视频拼接技术的研究和实现(一)   一直有计划研究实时图像拼接,但是直到最近拜读西电2013年张亚娟的《基于SURF特征的图像与视频拼接技术的研究和实......
  • 【数字图像处理复习】一些知识总结
    人眼中的图像马赫带效应:感知亮度并不是简单的灰度函数上冲,下冲:感觉黑的更黑,灰的更白同时对比效应:一个区域的感知亮度并不止取决于它的灰度,还取决于它周围物体的灰度......
  • 【python版CV】-图像处理(1)
    开头一下:上一篇学习完了图像的基础操作,这一篇博客是记录图像处理的一部分。回顾python版的OpenCV第二篇,对于相关的图像处理也有一定的了解。视频参考B站唐宇迪博士,也是来自......
  • 【图像处理基础】YUV格式理解
     1.YUV数据格式简介YUV,是一种颜色编码方法。常使用在各个视频处理组件中。“Y”表示明亮度(Luminance、Luma),“U”和“V”则是色度、浓度(Chrominance、Chroma)。NV......
  • Java+Swing可视化图像处理软件
    @目录一、系统介绍二、功能展示1.图片裁剪2.图片缩放3.图片旋转4.图像灰度处理5.图像变形6.图像扭曲7.图像移动三、系统实现1.ImageProcessing.java四、获取源码一、系统......
  • TensorFlow图像处理函数
    1.读取图片importmatplotlib.pyplotaspltimporttensorflowastfimportnumpyasnpimage_raw_data=tf.gfile.FastGFile('./datasets/cat.png','rb').read()withtf.......
  • Python Pillow(PIL) 图像处理(分离、合并、裁剪、几何变换)
    Pillow(PIL)是Python平台事实上的图像处理标准库,支持多种格式,并提供强大的图形与图像处理功能。PIL模块全称为PythonImagingLibrary,是Python中一个免费的图像处理模块......