我如何使用OpenCV裁剪图像,就像我之前在PIL中所做的那样.
关于PIL的工作示例
im = Image.open('0.png').convert('L')
im = im.crop((1, 1, 98, 33))
im.save('_0.png')
Run Code Online (Sandbox Code Playgroud)
但是我怎么能在OpenCV上做到这一点?
这是我试过的:
im = cv.imread('0.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
(thresh, im_bw) = cv.threshold(im, 128, 255, cv.THRESH_OTSU)
im = cv.getRectSubPix(im_bw, (98, 33), (1, 1))
cv.imshow('Img', im)
cv.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我想我错误地使用了getRectSubPix
.如果是这种情况,请解释我如何正确使用此功能.
下图将告诉你我想要什么.
我有图像中的矩形信息,宽度,高度,中心点和旋转度.现在,我想编写一个脚本来剪切它们并将它们保存为图像,但要理顺它们.因为我想从图像内部显示的矩形转到外面显示的矩形.
我正在使用OpenCV python,请告诉我一种方法来实现这一目标.
请显示一些代码作为OpenCV Python的例子很难找到.
我正在尝试开发用于车牌识别的简单PC应用程序(Java + OpenCV + Tess4j).图像并不是很好(进一步说它们会很好).我想为tesseract预处理图像,而我却坚持检测车牌(矩形检测).
我的步骤:
1)源图像
Mat img = new Mat();
img = Imgcodecs.imread("sample_photo.jpg");
Imgcodecs.imwrite("preprocess/True_Image.png", img);
Run Code Online (Sandbox Code Playgroud)
2)灰度
Mat imgGray = new Mat();
Imgproc.cvtColor(img, imgGray, Imgproc.COLOR_BGR2GRAY);
Imgcodecs.imwrite("preprocess/Gray.png", imgGray);
Run Code Online (Sandbox Code Playgroud)
3)高斯模糊
Mat imgGaussianBlur = new Mat();
Imgproc.GaussianBlur(imgGray,imgGaussianBlur,new Size(3, 3),0);
Imgcodecs.imwrite("preprocess/gaussian_blur.png", imgGaussianBlur);
Run Code Online (Sandbox Code Playgroud)
4)自适应阈值
Mat imgAdaptiveThreshold = new Mat();
Imgproc.adaptiveThreshold(imgGaussianBlur, imgAdaptiveThreshold, 255, CV_ADAPTIVE_THRESH_MEAN_C ,CV_THRESH_BINARY, 99, 4);
Imgcodecs.imwrite("preprocess/adaptive_threshold.png", imgAdaptiveThreshold);
Run Code Online (Sandbox Code Playgroud)
这应该是第5步,即检测板区域(现在可能甚至没有偏斜).
我使用Paint从图像(第4步之后)开始需要区域,并得到:
然后我做了OCR(通过tesseract,tess4j):
File imageFile = new File("preprocess/adaptive_threshold_AFTER_PAINT.png");
ITesseract instance = new Tesseract();
instance.setLanguage("eng");
instance.setTessVariable("tessedit_char_whitelist", "acekopxyABCEHKMOPTXY0123456789");
String result = instance.doOCR(imageFile);
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
并得到(足够好?)结果 - "Y841ox EH"(几乎是真的)
如何在第4步后检测并裁剪板区?我是否需要在1-4步中进行一些更改(改进)?希望看到一些通过Java …
我正在尝试稳健地提取轮廓的旋转边界框。我想拍摄一张图像,找到最大的轮廓,得到它的旋转边界框,旋转图像使边界框垂直,然后裁剪到大小。
为了演示,这是在以下代码中链接的原始图像。我想最终将那只鞋旋转到垂直并裁剪成尺寸。此答案中的以下代码似乎适用于 opencv 线条等简单图像,但不适用于照片。
最终结果是旋转和裁剪错误:
编辑:将阈值类型更改为 后cv2.THRESH_BINARY_INV
,它现在正确旋转但裁剪错误:
import cv2
import matplotlib.pyplot as plt
import numpy as np
import urllib.request
plot = lambda x: plt.imshow(x, cmap='gray').figure
url = 'https://i.imgur.com/4E8ILuI.jpg'
img_path = 'shoe.jpg'
urllib.request.urlretrieve(url, img_path)
img = cv2.imread(img_path, 0)
plot(img)
threshold_value, thresholded_img = cv2.threshold(
img, 250, 255, cv2.THRESH_BINARY)
_, contours, _ = cv2.findContours(thresholded_img, 1, 1)
contours.sort(key=cv2.contourArea, reverse=True)
shoe_contour = contours[0][:, 0, :]
min_area_rect = cv2.minAreaRect(shoe_contour)
def crop_minAreaRect(img, rect):
# rotate img
angle = rect[2]
rows, cols = …
Run Code Online (Sandbox Code Playgroud) 问题是如何使用OpenCV旋转图像并保持原始尺寸.目前使用此功能:
def rotateImage(image, angle):
(h, w) = image.shape[:2]
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center,angle,1.0)
rotated_image = cv2.warpAffine(image, M, (w,h))
return rotated_image
Run Code Online (Sandbox Code Playgroud)
另外warpAffine中使用了哪种算法(Bicubic?)
我想裁剪图像中最大的对象(字符)。该代码仅在没有行的情况下才有效(如第一个图像所示)。但是我需要忽略这条线并制作第二张图像。仅裁剪最大的对象图像。
import cv2
x1, y1, w1, h1 = (0,0,0,0)
points = 0
# load image
img = cv2.imread('Image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # convert to grayscale
# threshold to get just the signature
retval, thresh_gray = cv2.threshold(gray, thresh=100, maxval=255, type=cv2.THRESH_BINARY)
# find where the signature is and make a cropped region
points = np.argwhere(thresh_gray==0) # find where the black pixels are
points = np.fliplr(points) # store them in x,y coordinates instead of row,col indices
x, y, w, h = cv2.boundingRect(points) …
Run Code Online (Sandbox Code Playgroud)