我正在为我的大学任务开发一个项目,其中包含我尝试使用Unity和Vuforia的AR部分。我希望得到一个简单的T形(或任何易于用户在身体部位上绘制的形状,如手)作为图像目标,因为我正在开发一个类似于inkHunter的应用程序。在这个应用程序中,他们有一个笑脸作为图像目标,当客户在身体上绘制笑脸并将相机放在上面时,相机找到并在其上显示所选的纹身设计。我尝试使用Vuforia SDK,但他们给出了图像目标的评级,所以我无法得到我想要的图像目标。我认为使用openCV是正确的方法,但它很难学,而且我花的时间也少了。我认为这不是一件大事,所以请尽量帮我解决这个问题。我想你明白我的想法。在inkHunter中,即使我在一张纸上绘制目标,他们也会在上面显示纹身。我需要相同的意思,我需要检测绘制的目标。如果你能在这种情况下帮助我,那就太棒了。谢谢。
目标可以是这样的,
我能够从图片中进行模板匹配,并将其应用于实时,这意味着我在框架中循环。但它似乎没有将模板与框架匹配,而且我意识到找到(簿记变量)总是无。
import cv2 as cv2
import numpy as np
import imutils
def main():
template = cv2.imread("C:\\Users\\Manthika\\Desktop\\opencvtest\\template.jpg")
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template, 50, 200)
(tH, tW) = template.shape[:2]
cv2.imshow("Template", template)
windowName = "Something"
cv2.namedWindow(windowName)
cap = cv2.VideoCapture(0)
if cap.isOpened():
ret, frame = cap.read()
else:
ret = False
# loop over the frames to find the template
while ret:
# load the image, convert it to grayscale, and initialize the
# bookkeeping variable to keep track of the matched region
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
found = None
# loop over the scales of the image
for scale in np.linspace(0.2, 1.0, 20)[::-1]:
# resize the image according to the scale, and keep track
# of the ratio of the resizing
resized = imutils.resize(gray, width=int(gray.shape[1] * scale))
r = gray.shape[1] / float(resized.shape[1])
# if the resized image is smaller than the template, then break
# from the loop
if resized.shape[0] < tH or resized.shape[1] < tW:
break
# detect edges in the resized, grayscale image and apply template
# matching to find the template in the image
edged = cv2.Canny(resized, 50, 200)
result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
# if we have found a new maximum correlation value, then update
# the bookkeeping variable
if found is None or maxVal > found[0]:
found = (maxVal, maxLoc, r)
print(found)
# unpack the bookkeeping variable and compute the (x, y) coordinates
# of the bounding box based on the resized ratio
print(found)
if found is None:
# just show only the frames if the template is not detected
cv2.imshow(windowName, frame)
else:
(_, maxLoc, r) = found
(startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
(endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))
# draw a bounding box around the detected result and display the image
cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.imshow(windowName, frame)
if cv2.waitKey(1) == 27:
break
cv2.destroyAllWindows()
cap.release()
if __name__ == "__main__":
main()
请帮我解决这个问题
我可以用OpenCV部分暗示你,但没有Unity和Vuforia,希望它可能有所帮助。
那么,我看到项目管道的方式:
我将假设目标在白纸上是一个黑色的“T”,它可能出现在纸张的不同位置,以及纸张本身可能会移动。
1.检测位置,大小和宽高比
首先,您需要检测纸张,因为您知道它的颜色和宽高比,您可以使用RGB / HSV阈值进行分割。您也可以尝试使用深度/机器学习(类似于R-CNN,HOG-SVM等类似的策略),但这需要时间。然后,您可以使用OpenCV中的findContours()函数来获取最大的对象。从轮廓中,您可以获得纸张的位置,大小和纵横比。
在那之后你做了同样的事情,但在纸上寻找“T”。在这里你可以使用模板匹配方法,只需用不同大小的预定义掩码扫描感兴趣区域,或者只重复上面的步骤。
一个有用的资源可能是this信用卡字符识别示例。它有一天帮了我很多:)
2.使用单应性来转换应放在原件上的图像
提取宽高比后,您将知道应出现在“T”顶部的大致尺寸和形状。这将允许您使用单应性来转换想要放在“T”上的图像。 Here是一个很好的开始,你也可以google其他一些来源,应该有很多,并且据我所知,OpenCV应该具有相应的功能。
转换后,我建议你使用插值,因为之后可能会有一些丢失的像素。
3.叠加将一个图像放在另一个上面
最后一步是浏览输入图像的所有像素,并将变换后的图像放在目标像素上。
希望这有帮助,祝你好运!:)