将RGB面膜转换为JSON for Mask R CNN
我有这个面具图像。
在此图像中,我想创建一个包含不同类的多边形信息的JSON文件。这里不同的颜色表示不同的类。
从此图像中,我想创建一个相应的JSON文件。我需要帮助。
我已经从此图像中计算了子掩码。我也从Stackoverflow那里获得了帮助。
代码如下:
def create_sub_masks(mask_image):
width, height = mask_image.size
# Initialize a dictionary of sub-masks indexed by RGB colors
sub_masks = {}
for x in range(width):
for y in range(height):
# Get the RGB values of the pixel
pixel = mask_image.getpixel((x,y))[:3]
# If the pixel is not black...
if pixel != (0, 0, 0):
# Check to see if we've created a sub-mask...
pixel_str = str(pixel)
sub_mask = sub_masks.get(pixel_str)
if sub_mask is None:
# Create a sub-mask (one bit per pixel) and add to the dictionary
# Note: we add 1 pixel of padding in each direction
# because the contours module doesn't handle cases
# where pixels bleed to the edge of the image
sub_masks[pixel_str] = Image.new('1', (width+2, height+2))
# Set the pixel value to 1 (default is 0), accounting for padding
sub_masks[pixel_str].putpixel((x+1, y+1), 1)
return sub_masks
因此,在此之后,我要写:
img = Image.open('sample.jpg')
im2 = create_sub_masks(img)
我想要一个JSON文件,其中:
{
"version": "5.0.1",
"flags": {},
"shapes": [
{
"label": "window",
"points": [
[
7.132653061224492,
62.755102040816325
],
[
16.316326530612244,
62.244897959183675
],
[
31.112244897959187,
63.26530612244898
],
[
30.091836734693878,
89.79591836734694
],
[
17.846938775510203,
90.05102040816327
],
[
6.622448979591837,
89.54081632653062
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
"label": "window",
"points": [
[
7.132653061224492,
62.755102040816325
],
[
16.316326530612244,
62.244897959183675
],
[
31.112244897959187,
63.26530612244898
],
[
30.091836734693878,
89.79591836734694
],
[
17.846938775510203,
90.05102040816327
],
[
6.622448979591837,
89.54081632653062
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
...........
在此JSON文件中,将根据我给定的颜色列表创建标签。
I have this mask image.
From this image, I want to create a JSON file containing the polygon information of different classes. Here different color indicates different classes.
From this image, I want to create a corresponding JSON file. I need help.
I have calculated the sub mask from this image. I took help from the stackoverflow for that as well.
The code is below:
def create_sub_masks(mask_image):
width, height = mask_image.size
# Initialize a dictionary of sub-masks indexed by RGB colors
sub_masks = {}
for x in range(width):
for y in range(height):
# Get the RGB values of the pixel
pixel = mask_image.getpixel((x,y))[:3]
# If the pixel is not black...
if pixel != (0, 0, 0):
# Check to see if we've created a sub-mask...
pixel_str = str(pixel)
sub_mask = sub_masks.get(pixel_str)
if sub_mask is None:
# Create a sub-mask (one bit per pixel) and add to the dictionary
# Note: we add 1 pixel of padding in each direction
# because the contours module doesn't handle cases
# where pixels bleed to the edge of the image
sub_masks[pixel_str] = Image.new('1', (width+2, height+2))
# Set the pixel value to 1 (default is 0), accounting for padding
sub_masks[pixel_str].putpixel((x+1, y+1), 1)
return sub_masks
So after this I am writing:
img = Image.open('sample.jpg')
im2 = create_sub_masks(img)
I want a JSON file where:
{
"version": "5.0.1",
"flags": {},
"shapes": [
{
"label": "window",
"points": [
[
7.132653061224492,
62.755102040816325
],
[
16.316326530612244,
62.244897959183675
],
[
31.112244897959187,
63.26530612244898
],
[
30.091836734693878,
89.79591836734694
],
[
17.846938775510203,
90.05102040816327
],
[
6.622448979591837,
89.54081632653062
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
"label": "window",
"points": [
[
7.132653061224492,
62.755102040816325
],
[
16.316326530612244,
62.244897959183675
],
[
31.112244897959187,
63.26530612244898
],
[
30.091836734693878,
89.79591836734694
],
[
17.846938775510203,
90.05102040816327
],
[
6.622448979591837,
89.54081632653062
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
...........
In this JSON file, the labels will be created based on my given colour list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论