简体   繁体   中英

Cropping rectangle from Image using Opencv python

Im trying to crop rectangle image from screenshot, background for image must be white, Im ending up having black,How can I change that? I want to make histogtam of rgb for the final image and It seems plotting only vertical line on zero, Any kind of help is very important: here is my code:

import cv2 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

image = cv2.imread(filename = "Screenshot from 2019-11-08 22-02-27.png")
mask = np.zeros(shape = image.shape, dtype = "uint8")
cv2.rectangle(img = mask, 
    pt1 = (0, 185), pt2 = (1900, 773), 
    color = (255, 255, 255), 
    thickness = -1)



maskedImg = cv2.bitwise_and(src1 = image, src2 = mask)
cv2.imwrite("processed.png", maskedImg)
plt.imshow(maskedImg)
plt.show()


plt.hist(maskedImg.ravel(),256,[0,256]); plt.show()


 import cv2 import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg image = cv2.imread(filename = "1.png") mask = np.zeros(shape = image.shape, dtype = "uint8") cv2.rectangle(img = mask, pt1 = (0, 185), pt2 = (1900, 773), color = (255, 255, 255), thickness = -1) maskedImg = cv2.bitwise_and(src1 = image, src2 = mask) maskedImg[np.where((maskedImg==[0,0,0]).all(axis=2))] = [255,255,255] cv2.imwrite("processed.png", maskedImg) plt.imshow(maskedImg) plt.show()

Convert the black pixel present in the image into white pixel
Original image在此处输入图像描述


Crop Image在此处输入图像描述

在此处输入图像描述

 color = ('b','g','r') for i,col in enumerate(color): histr = cv2.calcHist([maskedImg],[i],None,[256],[0,256]) plt.plot(histr,color = col) plt.xlim([0,256]) plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM