对图像的处理,一般是对图像的numpy进行操作,即每个像素点。
这里设计的操作有and,or,not
还有+-*/
,我们这里得提前知道,像素点越大,那么颜色越亮,0代表黑色,255代表白色。
首先我们看四大基本操作。+-*/
def add_op(src1, src2): dst = cv.add(src1,src2) cv.imshow("add_op", dst) def sub_op(src1, src2): dst = cv.subtract(src1, src2) cv.imshow("sub_op", dst) def mul_op(src1, src2): dst = cv.multiply(src1, src2) cv.imshow("multipy_op", dst) def div_op(src1, src2): dst = cv.divide(src1, src2) cv.imshow("divide_op", dst)
+操作
这里我们分析为什么会出现这种情况,因为黑色为0,所有最右边的图黑色部分还是会保留原貌,但是白色部分为255,所以相加以后还是会保留为255,为白色。
-操作
和上面分析一样
*操作
/操作
按位的与或非
代码:
def bitwise_and(src1, src2): dst = cv.bitwise_and(src1, src2) cv.imshow("bitwise_and", dst) def bitwise_or(src1, src2): dst = cv.bitwise_or(src1, src2) cv.imshow("bitwise_or", dst) def bitwise_not(src1): dst = cv.bitwise_not(src1) cv.imshow("bitwise_not", dst)
按位与&
首先我们分析结果,按位&,那么为白色的地方,按位&以后,会保留另外一张图片的颜色,黑色地方还是继续为黑色。
我们看结果:
按位或|
首先想想安慰|,那么为白色的地方,肯定继续为白色,为黑色的地方,那么为另一张图片的内容。
按位非 not
最后的图像增强部分。
首先我们看具体的函数。
def contrast_brightness_demo(image, c, b): h, w, ch = image.shape[:] blank = np.zeros([h, w, ch], image.dtype) print(image[0, :5, 0]) dst = cv.addWeighted(image, c, blank, 1-c, b) print(dst[0, :5, 0]) cv.imshow("contrast_brightness_demo", dst)
我们看cv.addWeighted()
我们看它的具体参数
def addWeighted(src1, alpha, src2, beta, gamma, dst=None, dtype=None): # real signature unknown; restored from __doc__ """ addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) -> dst . @brief Calculates the weighted sum of two arrays. . . The function addWeighted calculates the weighted sum of two arrays as follows: . \f[\texttt{dst} (I)= \texttt{saturate} ( \texttt{src1} (I)* \texttt{alpha} + \texttt{src2} (I)* \texttt{beta} + \texttt{gamma} )\f] . where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each . channel is processed independently. . The function can be replaced with a matrix expression: . @code{.cpp} . dst = src1*alpha + src2*beta + gamma; . @endcode . @note Saturation is not applied when the output array has the depth CV_32S. You may even get . result of an incorrect sign in the case of overflow. . @param src1 first input array. . @param alpha weight of the first array elements. . @param src2 second input array of the same size and channel number as src1. . @param beta weight of the second array elements. . @param gamma scalar added to each sum. . @param dst output array that has the same size and number of channels as the input arrays. . @param dtype optional depth of the output array; when both input arrays have the same depth, dtype . can be set to -1, which will be equivalent to src1.depth(). . @sa add, subtract, scaleAdd, Mat::convertTo """ pass
dst = src1*alpha + src2*beta + gamma;
具体的就是看这个公式,但是这是一个矢量式。
我们看看最后的效果
图像的均值和方差
def others(m1, m2): M1 = cv.mean(m1) M2 = cv.mean(m2) print(M1) print(M2) mean1, dev1 = cv.meanStdDev(m1) print("图像m1的均值: %s, 方差为: %s" % (mean1, dev1))
所以熟悉两个函数
cv.mean() #返回均值 cv.meanStdDev() # 返回均值和方差
参考资料: