我正在尝试在图像中找到带圆角的矩形对象的轮廓.我试过HoughLinesP和findContours,但并没有达到预期的效果.
码:
import cv2
import matplotlib.pyplot as plt
import util
image = cv2.imread("./img/findrect0.png", 1)
gray = util.grayImage(image)
edges = cv2.Canny(image, 50, 200)
lines = cv2.HoughLinesP(edges, 1, cv2.cv.CV_PI/180, 50, minLineLength=50, maxLineGap=10)[0]
linesImage = image.copy()
util.drawLines(linesImage, lines, thickness=10)
contoursImage = image.copy()
(contours, hierarchy) = cv2.findContours(gray.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
util.drawContours(contoursImage, contours, thickness=10)
util.showOpenCVImagesGrid([image, edges, linesImage, contoursImage], 2, 2, titles=["original image", "canny image", "lines image", "contours image"])
Run Code Online (Sandbox Code Playgroud)
UTIL:
import cv2
import math
import matplotlib.pyplot as plt
def showOpenCVImagesGrid(images, x, y, …Run Code Online (Sandbox Code Playgroud) 使用class_addMethod代码:
class_addMethod(newClass, @selector(inputAccessoryView), accessoryImp, "@@:");\nRun Code Online (Sandbox Code Playgroud)\n\n该方法中参数“@@:”的含义是什么?
\n\n文档:
\n\n/** \n * Adds a new method to a class with a given name and implementation.\n * \n * @param cls The class to which to add a method.\n * @param name A selector that specifies the name of the method being added.\n * @param imp A function which is the implementation of the new method. The function must take at least two arguments\xe2\x80\x94self and _cmd.\n * …Run Code Online (Sandbox Code Playgroud) 我正在使用多线程来处理图像。
它在我有足够内存的计算机上运行良好(处理大量图像时增加2~3 GB),但我的服务器只有1GB内存,代码无法正常运行。
有时以 结尾Segmentation fault,有时:
Exception in thread Thread-13:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "passportRecognizeNew.py", line 267, in doSomething
...
Run Code Online (Sandbox Code Playgroud)
代码:
import threading
def doSomething(image):
# picture processing code
print("processing over")
threads = []
for i in range(20):
thread = threading.Thread(target=doSomething, args=("image",))
threads.append(thread)
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print("All over")
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题或有什么方法可以控制内存使用?
python multithreading memory-management image-processing out-of-memory