python opencv图像处理
程序员文章站
2024-01-20 16:51:34
...
# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt
import cv2
def roiProcess():
file = "F:\\dataSet\\picture\\000006 (2).jpg"
img=cv2.imread(file)
# [startRow: endRow, startCol: endCol]
roi = img[100:400, 0:200]
cv2.imshow("image", img)
cv2.imshow("roi", roi)
roi[:] = 0 # roi与原图像共享内存空间
cv2.imshow("image_", img)
cv2.waitKey()
def split_merae_pic():
file = "F:\\dataSet\\picture\\000006 (2).jpg"
img=cv2.imread(file)
b,g,r=cv2.split(img)
img_merge=cv2.merge([b,g,r])
# img_merge = [b, g, r] # error
zeros = np.zeros(img.shape[:2], dtype = "uint8")
img_merge_r = cv2.merge([zeros,zeros,r])
img_merge_g = cv2.merge([zeros,g,zeros])
img_merge_b = cv2.merge([b,zeros,zeros])
cv2.imshow("image", img)
cv2.imshow("img_b", b)
cv2.imshow("img_g", g)
cv2.imshow("img_r", r)
cv2.imshow("merge", img_merge)
cv2.imshow("merge_r", img_merge_r)
cv2.imshow("merge_g", img_merge_g)
cv2.imshow("merge_b", img_merge_b)
img[:,:,2]=0
cv2.imshow("merge_b_g", img)
cv2.waitKey()
def baseImageProcess():
file = "F:\\dataSet\\picture\\000006 (2).jpg"
img=cv2.imread(file)
# get image property
print(type(img))
print(img.shape) # 3D array
print("img.size=", img.size)
print("img.hight=", img.shape[0])
print("img.width=", img.shape[1])
print("img.channel=", img.shape[2])
print("img.dtype=", img.dtype)
px=img[100,100]
print(px)
for pixValue in px:
print(pixValue)
blue=img[100,100,0]
green = img[100, 100, 1]
red = img[100, 100, 2]
print('%d, %d, %d' % (blue, green, red))
print(img[100,100,:])
img[100,100]=[255,255,255]
print()
print(img.item(10, 10, 0))
print(img.item(10, 10, 1))
print(img.item(10, 10, 2))
print(img[10, 10])
img.itemset((10,10,0),255)
img.itemset((10,10,1),255)
img.itemset((10,10,2),255)
cv2.imshow("img", img)
cv2.waitKey()
# from matplotlib import pyplot as plt
def makeBorder():
file = "F:\\dataSet\\picture\\000006 (2).jpg"
img1=cv2.imread(file)
BLUE=[255,0,0]
replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)
constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()
def main():
baseImageProcess()
# roiProcess()
# split_merae_pic()
#makeBorder()
if __name__ == '__main__':
main()