我有一台照相机从上方指向禅宗花园。但是,相机固定在侧面,而不是直接固定在平板上方。结果,图像看起来像这样(注意矩形的偏斜形状):

有没有一种方法可以处理图像,以使沙子区域或多或少看起来像一个完美的正方形?
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.flip(img,0)
cv2.imshow('Cropping', img)
if cv2.waitKey(500) & 0xff == 27:
cv2.destroyAllWindows()
break
Run Code Online (Sandbox Code Playgroud)
非常感谢。
因此,我设法使用 opencv C++ 代码检测文档边框。现在,我需要裁剪该图像并使其看起来不错。
正如您在这张图片中看到的那样,边界检测看起来不错:
但是现在,当我尝试裁剪它时,它看起来像这样:
现在,这是我使用的代码:
extern "C"
JNIEXPORT void JNICALL
Java_prisca_ctest_ScanActivity_doWithMat(JNIEnv *env, jobject instance, jlong matNumber) {
//reference to the image from Java
Mat &image = *(Mat *) matNumber;
//resize image
image = GetSquareImage(image);
//add color effects for better detection
Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);
GaussianBlur(gray, gray, Size(5, 5), 0);
Mat edged;
Canny(gray, edged, 75, 200);
//find contours and sort them from biggest to smallest
vector<vector<Point> > contours;
vector<Point> screenCnt;
findContours(edged, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
// sort contours
sort(contours.begin(), …Run Code Online (Sandbox Code Playgroud)