crop image using OpenCV
OpenCV accepts an image you need to cut out a shape from it by coordinates and remove the excess how can this be implemented?
And is it possible to cut multiple images and combine them into one?
0
1 answers
Try this way:
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel
from PyQt5.QtGui import QPixmap, QImage, qRgb
from PyQt5.QtCore import Qt
import numpy as np
import cv2
app = QApplication(sys.argv)
gray_color_table = [qRgb(i, i, i) for i in range(256)]
def NumpyToQImage(im):
qim = QImage()
if im is None:
return qim
if im.dtype == np.uint8:
if len(im.shape) == 2:
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_Indexed8)
qim.setColorTable(gray_color_table)
elif len(im.shape) == 3:
if im.shape[2] == 3:
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_RGB888)
elif im.shape[2] == 4:
qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_ARGB32)
return qim
img = cv2.imread('D:/_Qt/img/pyqt.jpg')
qimg = NumpyToQImage(img)
assert(not qimg.isNull())
label = QLabel()
pixmap = QPixmap(qimg)
pixmap.scaled(200, 200)
label.setPixmap(pixmap)
label.show()
label_1 = QLabel()
pixmap = QPixmap('D:/_Qt/img/pyqt.jpg')
label_1.setPixmap(pixmap)
label_1.setScaledContents(True)
label_1.show()
img = cv2.imread('D:/_Qt/img/pyqt.jpg')
img = np.copy(img[0:90, 140:224, :])
qimg2 = NumpyToQImage(img)
assert(not qimg2.isNull())
label_2 = QLabel()
label_2.resize(224, 224)
pixmap = QPixmap(qimg2)
pixmap.scaled(300, 300, Qt.IgnoreAspectRatio, Qt.FastTransformation)
label_2.setPixmap(pixmap)
label_2.setScaledContents(True)
label_2.show()
app.exec_()
Or so:
import cv2
import numpy as np
resim = cv2.imread("img1.jpg")
cv2.imshow("img1.jpg", resim)
Area = resim[100:300, 100:320]
cv2.imshow("Area -> img1.jpg", Area)
cv2.waitKey(0)
cv2.destroyAllWindows()
Or so:
import cv2
import numpy as np
image = cv2.imread('im.png', -1)
mask = np.zeros(image.shape, dtype=np.uint8)
roi_corners = np.array([[(200,10), (400,10),
(400,100), (600,100),
(600,200), (400,200),
(400,300), (200,300),
(200,200), (10,200),
(10,100), (200,100),
]],
dtype=np.int32)
channel_count = image.shape[2]
ignore_mask_color = (255,)*channel_count
cv2.fillPoly(mask, roi_corners, ignore_mask_color)
masked_image = cv2.bitwise_and(image, mask)
cv2.imwrite('image_masked.png', masked_image)
cv2.imshow("image_masked.png", masked_image)
cv2.imshow("img1.jpg", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
image = cv2.imread('im.png', -1)
mask = np.zeros(image.shape, dtype=np.uint8)
roi_corners = np.array([[(200,10), (400,10),
(400,100), (600,100),
(600,200), (400,200),
(400,300), (200,300),
(200,200), (10,200),
(10,100), (200,100),
]],
dtype=np.int32)
channel_count = image.shape[2]
ignore_mask_color = (255,)*channel_count
cv2.fillPoly(mask, roi_corners, ignore_mask_color)
masked_image = cv2.bitwise_and(image, mask)
cv2.imwrite('image_masked.png', masked_image)
cv2.imshow("img1.jpg", image)
cv2.imshow("image_masked.png", masked_image)
### vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
image = cv2.imread("image_masked.png")
r = 200.0 / image.shape[1]
dim = (200, int(image.shape[0] * r))
resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
lower_white = np.array([0, 0, 0], dtype=np.uint8)
upper_white = np.array([0, 0, 0], dtype=np.uint8)
mask = cv2.inRange(resized, lower_white, upper_white)
res = cv2.bitwise_not(resized, resized, mask)
cv2.imshow('res', res)
### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.waitKey(0)
cv2.destroyAllWindows()
2
Author: S. Nick, 2019-07-25 09:02:49