设法做到这一点的是Python,但无法转换为Android。有人可以指出我的错误吗? 目标:裁剪图像以仅显示屏幕。然后裁剪后,屏幕外的所有内容都会设置为黑色。
print('Please input file name with extension:')
image = cv2.imread(input(), cv2.IMREAD_COLOR)
image2 = image.copy() #make copy for 2nd contour plot
scale_factor = 800 #input how big you want the image to be scaled down to here
original_image = scale_image(image.copy(), scale_factor)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
v = np.median(gray)
sigma = 0.33
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(gray, lower, upper)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(9,9))
dilated = cv2.dilate(edged, kernel)
contours, hierarchy = cv2.findContours(dilated.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Number of Contours found = " + str(len(contours)))
cnts = sorted(contours, key=cv2.contourArea, reverse = True)[:10]
screen_cnt = None
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.015 * peri, True)
area = cv2.contourArea(cnts[1])
if len(approx) == 4:
screen_cnt = approx
break
rect = cv2.boundingRect(screen_cnt)
x,y,w,h = rect
croped = image[y:y+h, x:x+w].copy()
screen_cnt = screen_cnt - screen_cnt.min(axis=0)
mask = np.zeros(croped.shape[:2], np.uint8)
cv2.drawContours(mask, [screen_cnt], -1, (255, 255, 255), -1, cv2.LINE_AA)
dst = cv2.bitwise_and(croped, croped, mask=mask)
cv2.drawContours(image2, [cnts[1]], -1, (0, 255, 0), 3)
contour_scaled3 = scale_image(image2, scale_factor)
cv2.imshow('Contours_of_screen', contour_scaled3)
static void sortContoursByArea(List<MatOfPoint> contours) {
for(int contourIdx=0; contourIdx < contours.size(); contourIdx++) {
double areaC = Imgproc.contourArea(contours.get(contourIdx));
if (areaC < 100000){
contours.remove(contourIdx);
}
}
Collections.sort(contours, (a, b) -> {
double areaA = Imgproc.contourArea(a);
double areaB = Imgproc.contourArea(b);
// Change sign depending on whether your want sorted small to big
// or big to small
if (areaA < areaB) {
return 1;
} else if (areaA > areaB) {
return -1;
}
return 0;
});
}
public void edgeDetection(View v)
{
Mat srcMat = new Mat (imageBitmap.getHeight(), imageBitmap.getWidth(), CvType.CV_8UC3);
Bitmap myBitmap32 = imageBitmap.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, srcMat);
Mat gray = new Mat(srcMat.size(), CvType.CV_8UC1);
Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGB2GRAY);
Mat edge = new Mat();
Mat edge2 = new Mat();
Mat edge3 = new Mat();
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Mat edge4 = new Mat();
Imgproc.bilateralFilter(gray, edge, 11, 17, 17);
Imgproc.Canny(edge, edge2, 80, 90);
Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5,5));
Imgproc.dilate(edge2, edge3, element);
Imgproc.findContours(edge3, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
sortContoursByArea(contours);
for(int c = 0; c < contours.size(); c++) {
MatOfPoint2f approxCurve = new MatOfPoint2f();
MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(c).toArray());
double approxDistance = Imgproc.arcLength(contour2f, true) * 0.015;
Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
if (approxCurve.toArray().length == 4) {
MatOfPoint points = new MatOfPoint(approxCurve.toArray());
// Get bounding rect of contour
Rect rect = Imgproc.boundingRect(points);
//Imgproc.rectangle(srcMat, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0, 255), 3);
edge4 = new Mat(srcMat, rect);
break;
}
}
// create Mat for mask
Mat mask = new Mat(new Size(edge4.cols(), edge4.rows() ), CvType.CV_8UC1);
mask.setTo(new Scalar(0.0));
// create Scalar for color of mask objects
Scalar red = new Scalar(255, 0, 0);
// draw contours border and fill them
Imgproc.drawContours(mask, contours, -1, red, 10);
for (MatOfPoint contour: contours) {
Imgproc.fillPoly(mask, Collections.singletonList(contour), red);
}
// create mat foe masked image
Mat masked = new Mat();
// apply mask to srcMat and set result to masked
edge4.copyTo(masked, mask);
//Imgproc.drawContours(srcMat, contours, -1, new Scalar(0, 255, 0), -1);
Bitmap resultBitmap = Bitmap.createBitmap(masked.cols(), masked.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(masked, resultBitmap);
imageView.setImageBitmap(resultBitmap);
}
图像在下面的链接中。 https://i.stack.imgur.com/63wdr.jpg