<jupyter_start><jupyter_text>图像基本操作 - 访问像素值并修改它们- 访问图像属性- 设置感兴趣区域（ROI）- 拆分和合并图像<jupyter_code>cd C:\Users\Administrator\Desktop
import numpy as np
import cv2

img = cv2.imread('test.jpg')<jupyter_output><empty_output><jupyter_text>获取并修改像素值<jupyter_code>#  it returns an array of Blue, Green, Red values
img[100, 100]
# For grayscale image, just corresponding intensity is returned
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray[100, 100]
img[101, 101] = [255, 255, 255]
img[101, 101]<jupyter_output><empty_output><jupyter_text>numpy是经过优化了的进行快速矩阵运算的包，所以不推荐逐个获取像素值并修改能矩阵运算就不要用循环。例如前5行的后3列，用numpy的array.item()和array.itemset()会更好。但是返回是标量，如果想获得所有RGB的值，需要使用array.item()分割他们。更好的方法是<jupyter_code>import cv2
import numpy

img = cv2.imread('test.jpg')
print(img.item(10, 10, 2))
img.itemset((10, 10, 2), 100)
print(img.item(10, 10, 2))<jupyter_output>210
100<jupyter_text>获取图像属性图像属性包括：行，列，通道，图像数据类型，像素数目等<jupyter_code>import cv2
import numpy

img = cv2.imread('test.jpg')
print(img.shape)
# 返回图像的像素数目
print(img.size)
# 数据类型
print(img.dtype)<jupyter_output>uint8<jupyter_text>图像ROI对图像的特定区域操作。ROI是使用numpy索引来获得的<jupyter_code>import cv2
import numpy

img = cv2.imread('roi.jpg')
print(img.shape)

ball = img[180:210, 30:60]
img[120:150, 90:120] = ball<jupyter_output>(212, 347, 3)<jupyter_text>拆分及合并图像通道 警告：cv2.split 时间消耗较大，需要时尽可能选择 Numpy 的索引操作会更快<jupyter_code>b, g, r = cv2.split(img)  #拆分
b
img = cv2.merge((b, g, r))  #合并<jupyter_output><empty_output><jupyter_text>或者<jupyter_code>b = img[:, :, 0]
b<jupyter_output><empty_output><jupyter_text>为图像扩边（填充） 想为图像周围建一个边可以使用**cv2.copyMakeBorder()**函数。这经常在**卷积运算或0填充时**被用到。具体参数如下：- 5.1 src输入图像- top,bottom,left,right对应边界的像素数目- borderType要添加哪种类型的边界：    - cv2.BORDER_CONSTANT添加有颜色的常数值边界，还需要下一个参数（value）    - cv2.BORDER_REFLIECT边界元素的镜像。例如：fedcba | abcdefgh | hgfedcb    - cv2.BORDER_101或者cv2.BORDER_DEFAULT跟上面一样，但稍作改动，例如：gfedcb | abcdefgh | gfedcba    - cv2.BORDER_REPLICATE重复最后一个元素。例如: aaaaaa| abcdefgh|hhhhhhh    - cv2.BORDER_WRAP 不知怎么了, 就像样: cdefgh| abcdefgh|abcdefg- value边界颜色<jupyter_code>import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

BLUE = [255, 0, 0]
img1 = cv.imread('rio.jpg')
replicate = cv.copyMakeBorder(img1, 10, 10, 10, 10, cv.BORDER_REPLICATE)
reflect = cv.copyMakeBorder(img1, 10, 10, 10, 10, cv.BORDER_REFLECT)
reflect101 = cv.copyMakeBorder(img1, 10, 10, 10, 10, cv.BORDER_REFLECT_101)
wrap = cv.copyMakeBorder(img1, 10, 10, 10, 10, cv.BORDER_WRAP)
constant = cv.copyMakeBorder(
    img1, 10, 10, 10, 10, cv.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()<jupyter_output><empty_output><jupyter_text>图像上的算术运算  图像加法使用cv2.add()将两幅图像进行加法运算，也可以直接使用numpy，res=img1+img2.两幅图像的大小，类型必须一致，或者第二个图像可以是一个简单的标量值。<jupyter_code>import cv2
import numpy as np

x=np.uint8([250])
y=np.uint8([10])

print(cv2.add(x,y))#250+10=260>=255
print (x+y)#250+10=260%255=4<jupyter_output>[4]<jupyter_text>OpenCV的结果会更好，so尽量使用OpenCV中的函数  图像混合这也是加法，不同的是两幅图像的权重不同，这会给人一种混合或者透明的感觉。图像混合的计算公式如下：g(x) = (1−α)f0 (x)+αf1 (x)通过修改α的值（0-->1）,可以实现很酷的混合。例：将两幅图像混合，第一幅权重为0.7.第二幅权重为0.3。函数cv2.addWeighed()可以按下面的公式对图片进行混合。dst = α·img1 + β·img2+γ这里γ的取值为0.```pythonimport cv2import numpy as npimg1 = cv2.imread('45.jpg')img2 = cv2.imread('messigray.png')dst = cv2.addWeighted(img1, 0.7, img2, 0.3, 0)cv2.imshow('dst', dst)cv2.waitKey(0)cv2.destroyAllWindows()```![](https://docs.opencv.org/4.1.0/blending.jpg)  按位运算这里包括按位操作有：AND，OR，NOT，XOR等，当我们提取图像的一部分，选择非矩形ROI时，会很有用（下章）。下面进行如何改变一幅图的特定区域。<jupyter_code>import cv2
import numpy as np

img1 = cv2.imread('45.jpg')
img2 = cv2.imread('messigray.png')

# I want to put logo on top-left corner, So I create a ROI
rows, cols, channels = img2.shape
roi = img1[0:rows, 0:cols]

# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# 二值化
ret, mask = cv2.threshold(img2gray, 175, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI
#取ROI中与mask中不为零的值对应的像素的值，其让值为0 。
#注意这里必须有mask=mask或者mask=mask_inv，其中mask=不能忽略
img1_bg = cv2.bitwise_and(roi, roi, mask=mask)

#取roi中与mask_inv中不为零的值对应的像素的值，其他值为0
# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2, img2, mask=mask_inv)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg, img2_fg)
img1[0:rows, 0:cols] = dst

cv2.imshow('res', img1)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>![](https://docs.opencv.org/4.1.0/overlay.jpg)  程序性能检测及优化  使用OpenCV检测程序效率 涉及函数 **cv2.getTickCount，cv2.getTickFrequency**1.使用OpenCV检测程序效率cv2.getTickCount函数返回从参考点到这个函数被执行的时钟数。在一个函数执行前后都调用它，可以得到这个函数的执行时间。cv2.getTickFrequency返回时钟频率，或者说每秒钟的时钟数例，窗口大小不同（5,7,9）的核函数来做中值滤波，查看一个函数运行了多少秒<jupyter_code>import cv2
import numpy as np

img = cv2.imread('roi.jpg')

e1 = cv2.getTickCount()
for i in range(5, 49, 2):
    img = cv2.medianBlur(img, i)
e2 = cv2.getTickCount()
time = (e2 - e1) / cv2.getTickFrequency()
print(time)
cv2.getTickFrequency()<jupyter_output><empty_output><jupyter_text>OpenCV中的默认优化cv2.useOptimized()来查看优化是否被开启，cv2.setUesOptimized()来开启优化。<jupyter_code>import cv2 
import numpy as np

# check if optimization is enabled 
cv2.useOptimized()
%timeit res = cv2.medianBlur(img, 49) 
# Disable it 
cv2.setUseOptimized(False)
cv2.useOptimized()
%timeit res = cv2.medianBlur(img,49)<jupyter_output>782 ns ± 77.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)<jupyter_text>在IPython中检测程序效率<jupyter_code>import cv2
import numpy as np

x =5
%timeit y=x**2 
%timeit y=x*x 
z = np.uint8([5])

%timeit y=z*z 
%timeit y=np.square(z)<jupyter_output>621 ns ± 53.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)<jupyter_text>**注意**：Python 的标算比 Nump 的标算快。对于仅包含一两个 元素的操作 Python 标比 Numpy 的数组快。但是当数组稍微大一点时 Numpy 就会胜出了<jupyter_code>%timeit z = cv2.countNonZero(img)
%timeit z = np.count_nonzero(img)<jupyter_output>1.25 µs ± 78.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
{'fraction_non_alphanumeric': 0.14404079031230083, 'fraction_numerical': 0.06389420012746973, 'mean_word_length': 7.205228758169935, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 52, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '440638', 'license': 'MIT', 'n_tokens_mistral': 3491, 'n_tokens_neox': 3441, 'n_words': 466, 'path': 'OpenCV/OpenCV-核心操作.ipynb', 'repo_name': 'wanZzz6/Modules-Learn', 'size': '6276'}