我想在java中的空白黑色图像上绘制线条和轮廓(从opencv查找轮廓中提取)并需要找到相交点。
或 我的目标是在图像上绘制轮廓和线条。我想知道直线和轮廓的交点。 如果可能的话,非常需要如何在 java 或 kotlin 中执行下面提到的 python 代码。
**# First, create a single channel image having circles drawn on it.
CircleImage = np.zeros((Height, Width), dtype=np.uint8)
CircleImage = cv2.circle(CircleImage, Center, Radius, 255, 1) # 255-color, 1-thickness
# Then create an image of the same size with only the line drawn on it
LineImage = np.zeros((Height, Width), dtype=np.uint8)
LineImage = cv2.line(LineImage, PointA, PointB, 255, 1) # 255-color, 1-thickness
# Perform bitwise AND operation
IntersectionImage = cv2.bitwise_and(CircleImage, LineImage)**
创建空白黑色图像并绘图会在 Java 中给出空白图像本身。
在Python中,它会是这样的:
import cv2
import numpy as np
height, width = 400, 400 # define size of image
circleImage = np.zeros((height, width), dtype=np.uint8) # define empty image for circle
lineImage = np.zeros((height, width), dtype=np.uint8) # define empty image for line
center = (200, 200) # do the circle in the middle
radius = 100 # define the radius
cv2.circle(circleImage, center, radius, 255, 2) # add the circle to the circleImage
pointA = (100, 100) # define points for line
pointB = (300, 300) # ==/==
cv2.line(lineImage, pointA, pointB, 255, 2) # add line to lineImage
intersectionImage = cv2.bitwise_and(circleImage, lineImage) # bitwise and to get intersections
contours, _ = cv2.findContours(intersectionImage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # find contours
for contour in contours: # for every contour
# get moments and print out the intersection
M = cv2.moments(contour)
if M['m00'] != 0:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
print(f"Intersected at: ({cx}, {cy})")
主要需要给直线和圆一定的厚度,以确保它们不会对角线穿过,从而找不到公共元素。您可以通过获取相交处检测到的轮廓的质心来校正厚度的影响。
对java确实没有帮助,尽管我想这对于java程序员来说是基本的