opencv操作(二)

一.像素逻辑运算:

如下图的一个取反(not)操作:

具体操作:

"像素逻辑运算"
import cv2 as cv
import numpy as np

def add_demo(m1,m2):
    dst = cv.add(m1,m2)
    cv.imshow('add',dst)

def subtract_demo(m1,m2):
    dst = cv.subtract(m1,m2)
    cv.imshow('subtract',dst)

def divide_demo(m1,m2):
    dst = cv.divide(m1,m2)
    cv.imshow('divide',dst)

def multiply_demo(m1,m2):
    dst = cv.multiply(m1,m2)
    cv.imshow('multipy',dst)
#均值
def mean_demo(m1,m2):
    m1 = cv.mean(m1)
    m2 = cv.mean(m2)
    print(m1,m2)

#与运算
def logic_and(m1,m2):
    dst = cv.bitwise_and(m1,m2)
    cv.imshow('logic-and',dst)
#或运算
def logic_or(m1,m2):
    dst = cv.bitwise_or(m1,m2)
    cv.imshow('logic-or',dst)

#非运算
def logic_not(m1,m2):
    dst1 = cv.bitwise_not(m1)
    dst2 = cv.bitwise_not(m2)
    cv.imshow('logic-not1',dst1)
    cv.imshow('logic-not2',dst2)
#对比度与亮度的调节

def contrast_bright_demo(image,c,b):
    h,w,ch = image.shape
    blank = np.zeros([h,w,ch],image.dtype)
    dst = cv.addWeighted(image,c,blank,1-c,b)
    cv.imshow('c_b',dst)
src1= cv.imread(r"D:\opencv-test\red.png")
src2= cv.imread(r"D:\opencv-test\black.png")
print(src1.shape)
print(src2.shape)
# cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("red",src1)
cv.imshow("black",src2)
# add_demo(src1,src2)
# multiply_demo(src1,src2)
# divide_demo(src1,src2)
# mean_demo(src1,src2)
# logic_and(src1,src2)
# logic_or(src1,src2)
logic_not(src1,src2)
# subtract_demo(src1,src2)
# contrast_bright_demo(src1,1.2,10)
cv.waitKey(0)
cv.destroyAllWindows()

二.ROI与泛洪填充

ROI:ROI区域即一张图上你感兴趣的区域,可以用数组切片的方式选出图像的特定区域,对ROI区域的操作,也会反应在原图上。

比如选取原图的一块区域操作后变成灰色:

代码:

import cv2 as cv
import  numpy as np



src= cv.imread(r"D:\opencv-test\red.png")
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)
print(src.shape)
face =src[50:100,50:100]
gray = cv.cvtColor(face,cv.COLOR_BGR2GRAY)
backface = cv.cvtColor(gray,cv.COLOR_GRAY2BGR) #还原三通道
src[50:100,50:100]=backface
# cv.imshow('face',face)
cv.imshow('face',src)

泛洪填充:即指定图中某一像素点,以该像素点为基准点,设置像素值的上限与下线,所有处于上下限范围内的像素,都会被填充为指定的新颜色。

代码如下:

import cv2 as cv
import  numpy as np

def fill_demo(image):
    image= image.copy()
    h,w = image.shape[:2]
    mask = np.zeros([h+2,w+2],np.uint8)
    cv.floodFill(image,mask,(30,30),(0,255,255),(100,100,100),(50,50,50),cv.FLOODFILL_FIXED_RANGE)
    # (0, 255, 255) yellow
    ''' 
FLOODFILL_FIXED_RANGE- 改变图像,泛洪填充         floodFill(image,mask,seedPoint,newVal,rect,loDiff,upDiff,flags)            seedPoint:种子像素,填充的起始点像素            newVal:要填充的颜色            loDiff:规定像素值的下限         upDiff:规定像素值的上限            seedPoint.BGR - loDiff <= 被填充像素值 <= seedPoint.BGR + upDiff         即 seedPoint的BGR各减去loDiff <= 要填充的颜色范围 <= seedPoint的BGR各加上upDiff     
''' cv.imshow('fill',image)

 

二值填充:通过限定mask中像素值为0的区域来规定填充区域。

def fill_binary():
    image = np.zeros([400,400,3],np.uint8)
    image[100:300,100:300,:] = 255
    cv.imshow('pre',image)
  
mask
= np.ones([402,402,1],np.uint8) mask[101:301,101:301] = 0 cv.floodFill(image,mask,(200,200),(0,0,255),cv.FLOODFILL_MASK_ONLY) cv.imshow('aft',image)

 

 注: 在进行二值填充是  mask层必须全部初始化为1 想填充的区域 初始化为0

 

posted @ 2019-07-09 19:06  纵横捭阖行  阅读(342)  评论(0编辑  收藏  举报