Opencv creates images, processes image pixel values, generates single-channel images and generates tif image methods.

I just made a small note so I can check it later.

1. Create a set size image

import numpy as np
"""h,w,c分别代表图像的高、宽和通道数"""
img = np.zeros((h,w,c),dtype = 'uint8')#全为0的图
img = np.zeros((h,w,c),dtype = 'uint8')#全为1的图
"""图像复制"""
img_x= img.copy()
"""查看图像尺寸等信息"""
img.shape
(img.shape[0],img.shape[1],img.shape[2])
img.size
"""通道分离,拆分通道 """
b, g, r = cv2.split(img)
"""合并通道""" 
m = cv2.merge([b, g, r]) 
cv2.imshow("Merge", m) 

2. Image pixel value processing

"""像素值为0的像素个数"""
len(img[img == 0])
"""某个通道上"""
img[img[:,:,0] == 0]
img[img[:,:,1] == 0]
"""某个点上的像素值"""
xx = img[h1,w1,c1]#像素坐标+通道
"""该区域设置为白色 ,y表示行的范围,x表示列的范围"""
img[y1:y2, x1:x2]=[255,255,255] 
img[100:200, 150:250] = [255,255,255] 
"""修改像素 """
img[88,142] = [255, 255, 255] 
"""分别获取 BGR通道像素""" 
blue = img[88,142,0] 

3. Save as single channel image

import cv2
"""三通道bgr转单通道gray"""
img=cv2.imread('breast_img1.png',cv2.IMREAD_GRAYSCALE)
cv2.imwrite('one.png',img)
"""直接获取某通道数据进行保存"""
cv2.imwrite(save_path,img[:,:,0])

4. About cv2.imwrite() and cv2.imread()

"""cv.imread后加上-1即可,表示按照图片原有格式进行读取如果没有加上-1图片会被自动转换为三通道图片"""
实验验证过,我以png格式保存了(imwrite)单通道的图像,用imread()读取保存的图片发现变成三通道了
用法:
cv2.imread(jpgfile_path,-1)
这样读出来就跟保存时的一致了
retval = imread(filename[, flags]) 

retval = imread(filename[, flags]) filename represents the path name of the image to be loaded, which supports Windows bitmaps, JPEG files, PNG images, portable file formats, Sun rasters raster files, TIFF files, HDR files, etc.
Flags is of type int, which represents the loading flag. It specifies the color type of a loaded image. The default value is 1. Among them, cv2.IMREAD_UNCHANGED means reading in the complete image or the image is immutable, including the alpha channel; cv2.IMREAD_GRAYSCALE means reading in the grayscale image; cv2.IMREAD_COLOR means reading in the color image, the default parameters, ignoring the alpha channel.

retval = imwrite(filename, img[, params]) 

filename represents the path and file name to be saved

img represents the image matrix

params represents the parameter encoding saved in a specific format, and the default value is empty. For JPEG images, this parameter (cv2.IMWRITE_JPEG_QUALITY) indicates the quality of the image, expressed as an integer from 0 to 100, and the default value is 95. For PNG images, this parameter (cv2.IMWRITE_PNG_COMPRESSION) represents the compression level, from 0 to 9. The higher the compression level, the smaller the image size. The default level is 3. For PPM, PGM, PBM pictures, this parameter represents a binary format flag (cv2.IMWRITE_PXM_BINARY) [2]. Note that the type is Long and must be converted to int.

5. Generate single-band tif image:

Practice summary for subsequent reference. The main goal is to generate tif images and have the processing effects displayed on the tif images.
Main ideas:
①Create a new tif based on the relevant information of the original tif image, including projection coordinate system, longitude and latitude, etc. ②The
cv method creates an image, which is essentially a digital matrix, used to operate pixel values
​​③Assign the value of the cv image to tif image, generated according to function configuration, etc.

import numpy as np
import os
import sys
from osgeo import gdal
import cv2

tif_out_path = './xxx/'
"""如果坐标系信息是继承自原有tif,假设通过一个读取函数gadl_info获取"""
img_width, img_height, img_nbands, img_proj,img_coord,dataset = gdal_info(image_file)
basename = os.path.basename(image_file)[:-4]#获取原图像的名称
tif_pre0 = np.zeros((img_height,img_width,3,dtype = 'uint8')
driver = gdal.GetDriverByName("GTiff")
tif_out_name = tif_out_path + basename + '.tif'
tif_pre = driver.Create(tif_out_name,img_width,img_height,1,gdal.GDT_Byte)
"""上面的“1”表示单波段,gdal.GDT_Byte是tif图像的8位像素深度"""
tif_pre.SetProjection(img_proj)
tif_pre.SetGeoTransform(img_coord)
"""上面两个是投影坐标系继承和坐标继承,左上角坐标值,如果不是继承,发生变化要重新计算"""
cv2.fillPoly(tif_pre0,[area1,area2],(255,255,255))
"""假如对某张cv图像进行区域的像素值填充操作,要把操作效果生成在tif图像上,如下"""
tif_pre.GetRasterBand(1).WriteArray(tif_pre0[:,:,-1])
del tif_pre

If a multi-band tif image is generated, the values ​​read on the channel are written sequentially.

"""加入要生成tif波段数为n"""
tif_pre = driver.Create(tif_out_name,img_width,img_height,n,gdal.GDT_Byte)
tif_pre0 = np.zeros((img_height,img_width,3,dtype = 'uint8')
"""如果得到的数字矩阵维度是3,那么按通道写入,显然可以得到波段数n为3的tif图像"""
for i in range(3):
	tif_pre.GetRasterBand(i+1).WriteArray(tif_pre0[:,:,i])
del tif_pre

Guess you like

Origin blog.csdn.net/qq_44442727/article/details/127146404