728x90
# Task 1 : 이미지속 한 픽셀에 접근, 수정
img = cv2.imread('daisy.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
px = img[100,100] # x,y 100,100위치의 픽셀값
print(px)
#blue
print(img[100,100,2]) # R G B 니까 0 1 2번째 채널 Blue값
#accessing RED value
img[100,100] = [255,255,255] # 100,100번째 픽셀값 하얀색으로 변경
print(img.item(100,100,0)) # 100,100,0번째 R값을 얻어옴 =255
img.itemset((100,100,0),200) #100,100,0번째 R값을 변경함 =200
print(img[100,100,0])# 200
# cv2.imshow('img',img)
# cv2.waitKey()
# cv2.destroyAllWindows()
# Task2 : 이미지 특성 접근
print(img.shape) #(285,425,3)
print(img.size)# 363375
print(img.dtype)#uint8
# Task3 : 이미지 ROI
roi = img[50: 230, 150:350]
img[0:180, 0:200] = roi
cv2.imshow('img',img)
cv2.waitKey()
cv2.destroyAllWindows()
# Task4 : 이미지 분할, 합병
b, g, r = cv2.split(img)
cv2.imshow('b',b)
cv2.imshow('g',g)
cv2.imshow('r',r)
img = cv2.merge((b,g,r))
cv2.imshow('img',img)
cv2.waitKey()
cv2.destroyAllWindows()
Task5 : 경계 만들기, 패딩
cv2.copyMakeBorder(영상, 상,하,좌.우 추가할 값, border타입(상수=픽셀값지정, reflect거울모드,등등))
replicate = cv2.copyMakeBorder(img,10,10,10,10,cv2.BORDER_REPLICATE)-
reflect = cv2.copyMakeBorder(img,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img,10,10,10,10,cv2.BORDER_WRAP)
constant= cv2.copyMakeBorder(img,10,10,10,10,cv2.BORDER_CONSTANT,value=(255,0,0))
cv2.imshow('replicate',replicate)
cv2.imshow('reflect',reflect)
cv2.imshow('reflect101',reflect101)
cv2.imshow('wrap',wrap)
cv2.imshow('constant',constant)
cv2.waitKey()
cv2.destroyAllWindows()
Task6 : 이미지 블렌딩, 산술연산
src1 = cv2.imread('Lena.jpg')
src2 = cv2.imread('peppers.png')
# cv2.addWeighted(영상1,가중치1, 영상2,가중치2, 추가적으로 더할 값)
dst = cv2.addWeighted(src1, 0.7, src2, 0.3, 0)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()
#Task7 : bitwise 연산
src1 = cv2.imread('st_changhak02.JPG')
src2 = cv2.imread('st_emblem01.jpg',cv2.IMREAD_UNCHANGED)
mask = cv2.imread('polygons.jpg', cv2.IMREAD_GRAYSCALE)
#마스크 가져오기
em_mask = src2[:,:,1]
_, em_mask = cv2.threshold(em_mask, 200,255, cv2.THRESH_BINARY_INV)
h, w = src2.shape[:2]
#roi 지정
roi = src1[0:h, 0:w]
# bitwise(영상1,영상2,출력영상,마스크)
cv2.bitwise_and(roi,src2,roi,em_mask)
cv2.imshow('bit_and', src1)
cv2.waitKey()
bit_or = cv2.bitwise_or(roi, src2, roi,em_mask)
cv2.imshow('bit_or', src1)
cv2.waitKey()
bit_xor = cv2.bitwise_xor(roi, src2, roi,em_mask)
cv2.imshow('bit_xor', src1)
cv2.waitKey()
bit_not = cv2.bitwise_not(mask)
cv2.imshow('bit_not', bit_not)
cv2.waitKey()
cv2.destroyAllWindows()
728x90
'ComputerVision' 카테고리의 다른 글
OpenCV_week07(DFT, Tamplate Matching) (0) | 2021.04.15 |
---|---|
OpenCV_week06(histogram, histBackProjection, DFT, FFT) (2) | 2021.04.13 |
OpenCV_week05(contours, image pyramid) (0) | 2021.04.13 |
OpenCV_week04(Thresholding, filtering, morphorogy, Canny) (0) | 2021.04.13 |
OpenCV_week03(Geometry) (0) | 2021.04.13 |