This commit is contained in:
leon 2025-02-27 17:31:43 +08:00
parent d6684ac2f2
commit 53dca80f0f
8 changed files with 797 additions and 292 deletions

BIN
bim_im.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

272
data_bim.json Normal file
View File

@ -0,0 +1,272 @@
[
{
"code": 1,
"type": "250x1450",
"x": 945,
"y": 0,
"center": 351,
"w": 1450,
"h": 250,
"angle": "0"
},
{
"code": 2,
"type": "300x300",
"x": 923,
"y": 0,
"center": 754,
"w": 300,
"h": 300,
"angle": "0"
},
{
"code": 3,
"type": "501x500",
"x": 1638,
"y": 0,
"center": 1054,
"w": 500,
"h": 501,
"angle": "0"
},
{
"code": 4,
"type": "250x648",
"x": 717,
"y": 0,
"center": 1149,
"w": 648,
"h": 250,
"angle": "0"
},
{
"code": 5,
"type": "250x250",
"x": 1337,
"y": 0,
"center": 1543,
"w": 250,
"h": 250,
"angle": "0"
},
{
"code": 6,
"type": "249x249",
"x": 1780,
"y": 0,
"center": 1750,
"w": 249,
"h": 249,
"angle": "0"
},
{
"code": 7,
"type": "400x399",
"x": 549,
"y": 0,
"center": 1955,
"w": 399,
"h": 400,
"angle": "0"
},
{
"code": 8,
"type": "300x302",
"x": 1143,
"y": 0,
"center": 2159,
"w": 302,
"h": 300,
"angle": "0"
},
{
"code": 9,
"type": "300x302",
"x": 1745,
"y": 0,
"center": 2157,
"w": 302,
"h": 300,
"angle": "0"
},
{
"code": 10,
"type": "248x1447",
"x": 2939,
"y": 0,
"center": 345,
"w": 1447,
"h": 248,
"angle": "0"
},
{
"code": 11,
"type": "298x297",
"x": 2947,
"y": 0,
"center": 747,
"w": 297,
"h": 298,
"angle": "0"
},
{
"code": 12,
"type": "499x499",
"x": 3640,
"y": 0,
"center": 1040,
"w": 499,
"h": 499,
"angle": "0"
},
{
"code": 13,
"type": "245x648",
"x": 2743,
"y": 0,
"center": 1146,
"w": 648,
"h": 245,
"angle": "0"
},
{
"code": 14,
"type": "247x247",
"x": 3338,
"y": 0,
"center": 1550,
"w": 247,
"h": 247,
"angle": "0"
},
{
"code": 15,
"type": "246x247",
"x": 3747,
"y": 0,
"center": 1757,
"w": 247,
"h": 246,
"angle": "0"
},
{
"code": 16,
"type": "398x392",
"x": 2548,
"y": 0,
"center": 1955,
"w": 392,
"h": 398,
"angle": "0"
},
{
"code": 17,
"type": "300x293",
"x": 3143,
"y": 0,
"center": 2147,
"w": 293,
"h": 300,
"angle": "0"
},
{
"code": 18,
"type": "298x297",
"x": 3750,
"y": 0,
"center": 2144,
"w": 297,
"h": 298,
"angle": "0"
},
{
"code": 19,
"type": "245x1447",
"x": 4940,
"y": 0,
"center": 344,
"w": 1447,
"h": 245,
"angle": "0"
},
{
"code": 20,
"type": "295x300",
"x": 4924,
"y": 0,
"center": 743,
"w": 300,
"h": 295,
"angle": "0"
},
{
"code": 21,
"type": "498x495",
"x": 5646,
"y": 0,
"center": 1030,
"w": 495,
"h": 498,
"angle": "0"
},
{
"code": 22,
"type": "245x648",
"x": 4726,
"y": 0,
"center": 1150,
"w": 648,
"h": 245,
"angle": "0"
},
{
"code": 23,
"type": "245x246",
"x": 5296,
"y": 0,
"center": 1541,
"w": 246,
"h": 245,
"angle": "0"
},
{
"code": 24,
"type": "245x245",
"x": 5741,
"y": 0,
"center": 1735,
"w": 245,
"h": 245,
"angle": "0"
},
{
"code": 25,
"type": "398x392",
"x": 4545,
"y": 0,
"center": 1955,
"w": 392,
"h": 398,
"angle": "0"
},
{
"code": 26,
"type": "298x296",
"x": 5141,
"y": 0,
"center": 2142,
"w": 296,
"h": 298,
"angle": "0"
},
{
"code": 27,
"type": "297x295",
"x": 5740,
"y": 0,
"center": 2146,
"w": 295,
"h": 297,
"angle": "0"
}
]

View File

@ -0,0 +1,202 @@
[
{
"label": 0,
"prob": 0.9336103200912476,
"x": 325,
"y": 217,
"width": 68,
"height": 71
},
{
"label": 0,
"prob": 0.9302101731300354,
"x": 561,
"y": 222,
"width": 56,
"height": 66
},
{
"label": 0,
"prob": 0.9269363284111023,
"x": 440,
"y": 115,
"width": 54,
"height": 55
},
{
"label": 0,
"prob": 0.9259827136993408,
"x": 495,
"y": 269,
"width": 41,
"height": 42
},
{
"label": 0,
"prob": 0.915646493434906,
"x": 430,
"y": 321,
"width": 166,
"height": 38
},
{
"label": 0,
"prob": 0.9102611541748047,
"x": 276,
"y": 94,
"width": 43,
"height": 41
},
{
"label": 0,
"prob": 0.9072883129119873,
"x": 451,
"y": 220,
"width": 80,
"height": 38
},
{
"label": 0,
"prob": 0.9049834609031677,
"x": 179,
"y": 323,
"width": 183,
"height": 38
},
{
"label": 0,
"prob": 0.8983769416809082,
"x": 512,
"y": 102,
"width": 40,
"height": 41
},
{
"label": 0,
"prob": 0.8979458212852478,
"x": 71,
"y": 216,
"width": 67,
"height": 66
},
{
"label": 0,
"prob": 0.8557734489440918,
"x": 579,
"y": 157,
"width": 31,
"height": 34
},
{
"label": 0,
"prob": 0.8004537224769592,
"x": 201,
"y": 218,
"width": 87,
"height": 39
},
{
"label": 0,
"prob": 0.7972114682197571,
"x": 195,
"y": 110,
"width": 52,
"height": 55
},
{
"label": 0,
"prob": 0.7899783849716187,
"x": 350,
"y": 97,
"width": 43,
"height": 41
},
{
"label": 0,
"prob": 0.789060115814209,
"x": 34,
"y": 93,
"width": 42,
"height": 39
},
{
"label": 0,
"prob": 0.7720834612846375,
"x": 573,
"y": 107,
"width": 37,
"height": 40
},
{
"label": 0,
"prob": 0.753940761089325,
"x": 247,
"y": 267,
"width": 46,
"height": 45
},
{
"label": 0,
"prob": 0.7526902556419373,
"x": 304,
"y": 170,
"width": 36,
"height": 34
},
{
"label": 0,
"prob": 0.7418428063392639,
"x": 103,
"y": 142,
"width": 37,
"height": 35
},
{
"label": 0,
"prob": 0.7005344033241272,
"x": 53,
"y": 167,
"width": 36,
"height": 36
},
{
"label": 0,
"prob": 0.6595599055290222,
"x": 353,
"y": 146,
"width": 38,
"height": 37
},
{
"label": 0,
"prob": 0.6562734842300415,
"x": 103,
"y": 91,
"width": 45,
"height": 41
},
{
"label": 0,
"prob": 0.6161856651306152,
"x": 540,
"y": 175,
"width": 33,
"height": 36
},
{
"label": 0,
"prob": 0.07473669946193695,
"x": 122,
"y": 33,
"width": 314,
"height": 358
},
{
"label": 0,
"prob": 0.056293122470378876,
"x": 0,
"y": 216,
"width": 43,
"height": 37
}
]

View File

@ -0,0 +1,202 @@
[
{
"label": 0,
"prob": 0.9336103200912476,
"x": 325,
"y": 217,
"width": 68,
"height": 71
},
{
"label": 0,
"prob": 0.9302101731300354,
"x": 561,
"y": 222,
"width": 56,
"height": 66
},
{
"label": 0,
"prob": 0.9269363284111023,
"x": 440,
"y": 115,
"width": 54,
"height": 55
},
{
"label": 0,
"prob": 0.9259827136993408,
"x": 495,
"y": 269,
"width": 41,
"height": 42
},
{
"label": 0,
"prob": 0.915646493434906,
"x": 430,
"y": 321,
"width": 166,
"height": 38
},
{
"label": 0,
"prob": 0.9102611541748047,
"x": 276,
"y": 94,
"width": 43,
"height": 41
},
{
"label": 0,
"prob": 0.9072883129119873,
"x": 451,
"y": 220,
"width": 80,
"height": 38
},
{
"label": 0,
"prob": 0.9049834609031677,
"x": 179,
"y": 323,
"width": 183,
"height": 38
},
{
"label": 0,
"prob": 0.8983769416809082,
"x": 512,
"y": 102,
"width": 40,
"height": 41
},
{
"label": 0,
"prob": 0.8979458212852478,
"x": 71,
"y": 216,
"width": 67,
"height": 66
},
{
"label": 0,
"prob": 0.8557734489440918,
"x": 579,
"y": 157,
"width": 31,
"height": 34
},
{
"label": 0,
"prob": 0.8004537224769592,
"x": 201,
"y": 218,
"width": 87,
"height": 39
},
{
"label": 0,
"prob": 0.7972114682197571,
"x": 195,
"y": 110,
"width": 52,
"height": 55
},
{
"label": 0,
"prob": 0.7899783849716187,
"x": 350,
"y": 97,
"width": 43,
"height": 41
},
{
"label": 0,
"prob": 0.789060115814209,
"x": 34,
"y": 93,
"width": 42,
"height": 39
},
{
"label": 0,
"prob": 0.7720834612846375,
"x": 573,
"y": 107,
"width": 37,
"height": 40
},
{
"label": 0,
"prob": 0.753940761089325,
"x": 247,
"y": 267,
"width": 46,
"height": 45
},
{
"label": 0,
"prob": 0.7526902556419373,
"x": 304,
"y": 170,
"width": 36,
"height": 34
},
{
"label": 0,
"prob": 0.7418428063392639,
"x": 103,
"y": 142,
"width": 37,
"height": 35
},
{
"label": 0,
"prob": 0.7005344033241272,
"x": 53,
"y": 167,
"width": 36,
"height": 36
},
{
"label": 0,
"prob": 0.6595599055290222,
"x": 353,
"y": 146,
"width": 38,
"height": 37
},
{
"label": 0,
"prob": 0.6562734842300415,
"x": 103,
"y": 91,
"width": 45,
"height": 41
},
{
"label": 0,
"prob": 0.6161856651306152,
"x": 540,
"y": 175,
"width": 33,
"height": 36
},
{
"label": 0,
"prob": 0.07473669946193695,
"x": 122,
"y": 33,
"width": 314,
"height": 358
},
{
"label": 0,
"prob": 0.056293122470378876,
"x": 0,
"y": 216,
"width": 43,
"height": 37
}
]

Binary file not shown.

After

Width:  |  Height:  |  Size: 435 KiB

View File

@ -1,272 +0,0 @@
[
{
"code": "1",
"type": "250x1450",
"x": "196.0",
"y": 0,
"center": "351",
"w": "1450",
"h": "250",
"angle": "0\u00b0"
},
{
"code": "2",
"type": "300x300",
"x": "174.0",
"y": 0,
"center": "754",
"w": "300",
"h": "300",
"angle": "0\u00b0"
},
{
"code": "3",
"type": "501x500",
"x": "889.5",
"y": 0,
"center": "1054.5",
"w": "500",
"h": "501",
"angle": "0\u00b0"
},
{
"code": "4",
"type": "250x648",
"x": "-32.0",
"y": 0,
"center": "1149",
"w": "648",
"h": "250",
"angle": "0\u00b0"
},
{
"code": "5",
"type": "250x250",
"x": "588.0",
"y": 0,
"center": "1543",
"w": "250",
"h": "250",
"angle": "0\u00b0"
},
{
"code": "6",
"type": "249x249",
"x": "1031.5",
"y": 0,
"center": "1750.5",
"w": "249",
"h": "249",
"angle": "0\u00b0"
},
{
"code": "7",
"type": "400x399",
"x": "-199.5",
"y": 0,
"center": "1955",
"w": "399",
"h": "400",
"angle": "0\u00b0"
},
{
"code": "8",
"type": "300x302",
"x": "394.0",
"y": 0,
"center": "2159",
"w": "302",
"h": "300",
"angle": "0\u00b0"
},
{
"code": "9",
"type": "300x302",
"x": "996.0",
"y": 0,
"center": "2157",
"w": "302",
"h": "300",
"angle": "0\u00b0"
},
{
"code": "10",
"type": "248x1447",
"x": "2190.0",
"y": 0,
"center": "345",
"w": "1447",
"h": "248",
"angle": "0\u00b0"
},
{
"code": "11",
"type": "298x297",
"x": "2198.5",
"y": 0,
"center": "747",
"w": "297",
"h": "298",
"angle": "0\u00b0"
},
{
"code": "12",
"type": "499x499",
"x": "2891.0",
"y": 0,
"center": "1040.5",
"w": "499",
"h": "499",
"angle": "0\u00b0"
},
{
"code": "13",
"type": "245x648",
"x": "1994.5",
"y": 0,
"center": "1146.5",
"w": "648",
"h": "245",
"angle": "0\u00b0"
},
{
"code": "14",
"type": "247x247",
"x": "2589.5",
"y": 0,
"center": "1550.5",
"w": "247",
"h": "247",
"angle": "0\u00b0"
},
{
"code": "15",
"type": "246x247",
"x": "2998.5",
"y": 0,
"center": "1757",
"w": "247",
"h": "246",
"angle": "0\u00b0"
},
{
"code": "16",
"type": "398x392",
"x": "1799.5",
"y": 0,
"center": "1955",
"w": "392",
"h": "398",
"angle": "0\u00b0"
},
{
"code": "17",
"type": "300x293",
"x": "2394.5",
"y": 0,
"center": "2147",
"w": "293",
"h": "300",
"angle": "0\u00b0"
},
{
"code": "18",
"type": "298x297",
"x": "3001.5",
"y": 0,
"center": "2144",
"w": "297",
"h": "298",
"angle": "0\u00b0"
},
{
"code": "19",
"type": "245x1447",
"x": "4191.5",
"y": 0,
"center": "344.5",
"w": "1447",
"h": "245",
"angle": "0\u00b0"
},
{
"code": "20",
"type": "295x300",
"x": "4175.0",
"y": 0,
"center": "743.5",
"w": "300",
"h": "295",
"angle": "0\u00b0"
},
{
"code": "21",
"type": "498x495",
"x": "4897.5",
"y": 0,
"center": "1030",
"w": "495",
"h": "498",
"angle": "0\u00b0"
},
{
"code": "22",
"type": "245x648",
"x": "3977.0",
"y": 0,
"center": "1150.5",
"w": "648",
"h": "245",
"angle": "0\u00b0"
},
{
"code": "23",
"type": "245x246",
"x": "4547.5",
"y": 0,
"center": "1541.5",
"w": "246",
"h": "245",
"angle": "0\u00b0"
},
{
"code": "24",
"type": "245x245",
"x": "4992.5",
"y": 0,
"center": "1735.5",
"w": "245",
"h": "245",
"angle": "0\u00b0"
},
{
"code": "25",
"type": "398x392",
"x": "3796.5",
"y": 0,
"center": "1955",
"w": "392",
"h": "398",
"angle": "0\u00b0"
},
{
"code": "26",
"type": "298x296",
"x": "4392.5",
"y": 0,
"center": "2142",
"w": "296",
"h": "298",
"angle": "0\u00b0"
},
{
"code": "27",
"type": "297x295",
"x": "4991.5",
"y": 0,
"center": "2146.5",
"w": "295",
"h": "297",
"angle": "0\u00b0"
}
]

View File

@ -1,4 +1,7 @@
import cv2
from utils import filter_points
print(cv2.__version__) # 4.9.0
import json
import math
@ -13,8 +16,9 @@ def get_params(num_param, start, end):
def parmas_to_num(text_param):
for item in text_param:
item['center'] = int(float(item['center']) * 1000)
item['x'] = int(item['x'])
# item['center'] = int(float(item['center']) * 1000)
item['center'] = int(float(item['center']))
item['x'] = int(float(item['x']))
item['w'] = int(item['w'])
item['h'] = int(item['h'])
return text_param
@ -275,7 +279,7 @@ if __name__ == "__main__":
# 读取并处理数据
data_bim = {}
data_bim["type"] = 0
data_bim["params"] = read_from_json("./params.json")
data_bim["params"] = read_from_json("data_bim.json")
data_bim["point"] = []
data_bim["params"] = parmas_to_num(data_bim["params"])
@ -284,21 +288,42 @@ if __name__ == "__main__":
data_sub["params"] = []
# 创建测试子集
sub_im = cv2.imread("wide_image.png")
# sub_im = cv2.imread("wide_image.png")
# sub_zero = np.zeros_like(sub_im)
# _im_gray = cv2.cvtColor(sub_im, cv2.COLOR_BGR2GRAY)
# _im_gray = cv2.GaussianBlur(_im_gray, (5, 5), 0)
# _im_edge_sobel = _sobel(_im_gray)
# _, _im_thresh = cv2.threshold(_im_edge_sobel, 5, 255, cv2.THRESH_BINARY)
# cnts = _findContours(_im_thresh)
# for contour in cnts:
# x, y, w, h = cv2.boundingRect(contour)
# cv2.rectangle(sub_zero, (x, y), (x + w, y + h), (255, 255, 255), -1)
# _im_edge_sobel = _sobel(sub_zero)
# _, _im_thresh = cv2.threshold(_im_edge_sobel, 5, 255, cv2.THRESH_BINARY)
# cnts = _findContours(_im_thresh)
sub_im = cv2.imread("data_sub/test_1/wide_image.png")
sub_im2 = cv2.imread("data_sub/test_1/wide_image.png")
sub_zero = np.zeros_like(sub_im)
_im_gray = cv2.cvtColor(sub_im, cv2.COLOR_BGR2GRAY)
_im_gray = cv2.GaussianBlur(_im_gray, (5, 5), 0)
_im_edge_sobel = _sobel(_im_gray)
_, _im_thresh = cv2.threshold(_im_edge_sobel, 5, 255, cv2.THRESH_BINARY)
cnts = _findContours(_im_thresh)
cnts = read_from_json("data_sub/test_1/data_sub.json")
# cnts的矩形画在sub_im 上
for i in range(len(cnts)):
p = cnts[i]
cv2.rectangle(sub_im2, (p['x'], p['y']), (p['x'] + p['width'], p['y'] + p['height']), (0, 0, 255), 2)
# 写编号
cv2.putText(sub_im2, str(i), (p['x'], p['y']), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
if (i == 15):
print("=========15=======")
print(p)
cv2.imshow("sub_im_before_filter", sub_im2)
# 过滤点
cnts = filter_points("data_sub/test_1/wide_image.png",cnts)
for contour in cnts:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(sub_zero, (x, y), (x + w, y + h), (255, 255, 255), -1)
_im_edge_sobel = _sobel(sub_zero)
_, _im_thresh = cv2.threshold(_im_edge_sobel, 5, 255, cv2.THRESH_BINARY)
cnts = _findContours(_im_thresh)
for contour in cnts:
x, y, w, h = cv2.boundingRect(contour)
# x, y, w, h = cv2.boundingRect(contour)
x = contour["x"]
y = contour["y"]
w = contour["width"]
h = contour["height"]
# 由于定位框大小大于预埋件大小,因此这里需要做缩放处理
k = int(h*0.2) # roi2
k = int(h*0.01) # roi1
@ -323,9 +348,13 @@ if __name__ == "__main__":
param['center'] = sub_im.shape[0] - int((param["y1"] + param["y3"]) / 2)
data_sub["params"].append(param)
cv2.rectangle(sub_zero, (x, y), (x + w, y + h), (0, 255, 0), 1)
# cv2.imshow("1", sub_zero)
bim_im = gen_im_from_params(data_bim["params"])
# cv2.namedWindow("bim", cv2.WINDOW_NORMAL)
# cv2.imshow("bim", bim_im)
cv2.imwrite("bim_im.png", bim_im)
# cv2.waitKey(0)
cv2.imshow("sub_zero", sub_zero)
cv2.waitKey(0)
# data_sub = {}
# data_sub["type"] = 0
@ -484,6 +513,15 @@ if __name__ == "__main__":
print(f"Execution time: {elapsed_time:.4f} seconds")
img_matches = cv2.resize(bim_im, (int(bim_im.shape[1]/6), int(bim_im.shape[0]/6)))
# sub_im = cv2.resize(sub_im, (int(sub_im.shape[1]/10), int(sub_im.shape[0]/10)))
cv2.imshow("1", img_matches)
cv2.imshow("2", sub_im)
cv2.imshow("2", img_matches)
# cnts的矩形画在sub_im 上
for i in range(len(cnts)):
p = cnts[i]
cv2.rectangle(sub_im, (p['x'], p['y']), (p['x']+p['width'],p['y']+p['height']), (0, 0, 255), 2)
# 写编号
cv2.putText(sub_im, str(i), (p['x'], p['y']), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
if(i == 15):
print("=========15=======")
print(p)
cv2.imshow("sub_im_after_filter", sub_im)
cv2.waitKey(0)

63
utils.py Normal file
View File

@ -0,0 +1,63 @@
import json
import logging
import cv2
def filter_points(image_path,points):
# 高度过大点过滤参数
max_height_rate = 0.5 # 矩形高度占整个画面高度的最大比例,如果超过该比例,则认为是无效矩形
# x范围过滤只保留中间部分的点只保留矩形完整出现在在画面中间的那部分矩形。
left_x_cut_rate=0.2 # 左边界的裁剪比例,从左边开始裁剪百分之多少
right_x_cut_rate=0.2 # 右边界的裁剪比例,从右边开始裁剪百分之多少
image = cv2.imread(image_path)
image_height = image.shape[0]
image_width = image.shape[1]
image_x_min = image_width * left_x_cut_rate
print(f"image_x_min====={image_x_min}")
image_x_max = image_width * (1 - right_x_cut_rate)
print(f"image_x_max====={image_x_max}")
#开始过滤点
bad_point_index = []
print(f'开始过滤点,原有点数为{len(points)}')
for index in range(len(points)):
point = points[index]
# 高度过大过滤
if point['height'] > image_height * max_height_rate:
bad_point_index.append(index)
continue
# x坐标范围过滤
# 原point中的x和y,是矩形左上角的坐标
x_min = point['x'] # 矩形四个点坐标中x的最小值
x_max = point['x'] + point['width'] # 矩形四个点坐标中x的最大值
# 如果矩形x的最小值小于左边界去除这个矩形
if x_min < image_x_min:
bad_point_index.append(index)
continue
# 如果矩形x的最大值大于右边界去除这个矩形
if x_max > image_x_max:
bad_point_index.append(index)
continue
print(f'过滤点结束,过滤之后的点数为{len(points)}')
# 删除bad_point_index
filtered_points = []
for i, point in enumerate(points):
if i not in bad_point_index:
filtered_points.append(point)
return filtered_points
# def read_from_json(file_path):
# with open(file_path, 'r') as f:
# loaded_array = json.load(f)
# return loaded_array
# cnts = read_from_json("data_sub/test_1/data_sub.json")
# filter_points("data_sub/test_1/wide_image.png",cnts)