Python cv2.error: OpenCV(3.4.1) error: (-215) !_src.empty() in function fastNlMeansDenoising when using calculated values inside array

Python cv2.error: OpenCV(3.4.1) error: (-215) !_src.empty() in function fastNlMeansDenoising when using calculated values inside array

我目前正在使用 OpenCV 和 PyTesseract 从指定的 ROI 中读取图像。这是我的经历:

import pytesseract
import urllib
import math
import cv2
import numpy as np
from unidecode import unidecode
from PIL import Image

def process_image(path=None):
    if path != None:
        original_image = cv2.imread(path)
        original_height, original_width = original_image.shape[:2]
        image = cv2.resize(original_image, (640, int(math.floor(original_height*640/original_width))))
        cv2.imwrite('./uploads/resized_image.jpg', image)
        height, width = image.shape[:2]

    else:
        return "Terjadi kesalahan, harap hubungi administrator."

    print(height)
    print(width)
    coord = [
        [height - 84, 34, height, 624] #Barcode
        # [378, 415, 364, 583], #Tanggal Lahir
        # [605, 656, 32, 992]
    ]

    result = None

    print ("Recognizing...")
    for x in coord:
        roi = image[x[0]:x[1],x[2]:x[3]]
        print(x[0])
        print(x[1])
        print(x[2])
        print(x[3])
        gray = cv2.cvtColor(roi,cv2.COLOR_RGB2GRAY)
        ret2,th2 = cv2.threshold(gray,128,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        dst = cv2.fastNlMeansDenoising(th2,10,10,7)
        cv2.imwrite('./uploads/tmp.jpg',dst)
        cao = Image.open('./uploads/tmp.jpg')
        rec_string = pytesseract.image_to_string(cao,lang='deu')
        if result != None:
            result += "\n" + rec_string
        else:
            result = rec_string

    print ("the result is {}".format(result))
    return result

在这里你可以看到,它获取了一张图片,将其调整到一定大小,并将图片写入一个名为resized_image.jpg的新图片中。然后,我试图从基于高度的坐标创建 ROI。看一下坐标数组,您可以看到我正在尝试通过从高度 - 84 到高度和宽度从 34 到 624 获取坐标来获取底部组件。

结果很好,在我打印出 "Recognizing..." 文本后,如果值是正确的。是的,但是图片不会被处理,弹出这个错误:

File ./uploads/24-07-2018/aenkjtWIBYuTZsaU.jpg telah diupload 
903
640
Recognizing...
819
34
903
624
OpenCV(3.4.1) Error: Assertion failed (!_src.empty()) in fastNlMeansDenoising, file /opt/conda/conda-bld/opencv-suite_1527005194613/work/modules/photo/src/denoising.cpp, line 119
[2018-07-24 13:51:29,900] ERROR in app: Exception on /api/upload [POST]
Traceback (most recent call last):
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/dennydap/Projects/flask-ocr/server.py", line 55, in upload_file
    rec_string = process_image(path=path)
  File "/home/dennydap/Projects/flask-ocr/pre_img.py", line 39, in process_image
    dst = cv2.fastNlMeansDenoising(th2,10,10,7)
cv2.error: OpenCV(3.4.1) /opt/conda/conda-bld/opencv-suite_1527005194613/work/modules/photo/src/denoising.cpp:119: error: (-215) !_src.empty() in function fastNlMeansDenoising

OpenCV(3.4.1) /opt/conda/conda-bld/opencv-suite_1527005194613/work/modules/photo/src/denoising.cpp:119: error: (-215) !_src.empty() in function fastNlMeansDenoising

127.0.0.1 - - [24/Jul/2018 13:51:29] "POST /api/upload HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/werkzeug/serving.py", line 270, in run_wsgi
    execute(self.server.app)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/werkzeug/serving.py", line 258, in execute
    application_iter = app(environ, start_response)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1748, in handle_exception
    return self.finalize_request(handler(e), from_error_handler=True)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1831, in finalize_request
    response = self.make_response(rv)
  File "/home/dennydap/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1957, in make_response
    'The view function did not return a valid response. The'
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

如果我使用确切的数字,而不是使用高度 - 84,则错误不会出现。我做错了什么?

原来ROI的定义是错误的。我以为是 roi = image[y1:x1, y2:x2] 但快速搜索后是 roi = image[y1:y2, x1:x2]。该睡觉了。