如何从 RCNN 中裁剪分割的对象?
Posted
技术标签:
【中文标题】如何从 RCNN 中裁剪分割的对象?【英文标题】:How to crop segmented objects from an RCNN? 【发布时间】:2020-01-31 09:42:20 【问题描述】:我正在尝试裁剪由 MASK RCNN 输出的分段对象,唯一的问题是,当我进行裁剪时,我得到的是带有遮罩颜色而不是带有遮罩颜色的片段它们的原始颜色。
这是带有片段的输出图像:
这是一个片段(我们在这张图片中有 17 个片段):
如您所见,我们有蒙版颜色而不是原始颜色的片段。
这是我正在使用的代码:
from mrcnn.config import Config
from mrcnn import model as modellib
from mrcnn import visualize
import numpy as np
import colorsys
import argparse
import imutils
import random
import cv2
import os
import matplotlib.image as mpimg
import cv2
import matplotlib.pyplot as plt
import numpy as np
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-w", "--weights", required=True,
help="path to Mask R-CNN model weights pre-trained on COCO")
ap.add_argument("-l", "--labels", required=True,
help="path to class labels file")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
ap.add_argument("-i", "--image", required=True,
help="path to input image to apply Mask R-CNN to")
args = vars(ap.parse_args())
# load the class label names from disk, one label per line
CLASS_NAMES = open(args["labels"]).read().strip().split("\n")
# generate random (but visually distinct) colors for each class label
# (thanks to Matterport Mask R-CNN for the method!)
hsv = [(i / len(CLASS_NAMES), 1, 1.0) for i in range(len(CLASS_NAMES))]
COLORS = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
random.seed(42)
random.shuffle(COLORS)
class SimpleConfig(Config):
# give the configuration a recognizable name
NAME = "fashion"
# set the number of GPUs to use along with the number of images
# per GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
NUM_CLASSES = 1 + 3
# Skip detections with < 90% confidence
DETECTION_MIN_CONFIDENCE = args["confidence"]
# initialize the inference configuration
config = SimpleConfig()
# initialize the Mask R-CNN model for inference and then load the
# weights
print("[INFO] loading Mask R-CNN model...")
model = modellib.MaskRCNN(mode="inference", config=config,
model_dir=os.getcwd())
model.load_weights(args["weights"], by_name=True)
# load the input image, convert it from BGR to RGB channel
# ordering, and resize the image
# default value 512 form the width
image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = imutils.resize(image, width=1150)
# perform a forward pass of the network to obtain the results
print("[INFO] making predictions with Mask R-CNN...")
r = model.detect([image], verbose=1)[0]
image = visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
['BG', 'top', 'boots' , 'bag'], r['scores'],
title="")
# get and then save the segmented objects
i = 0
mask = r["masks"]
for i in range(mask.shape[2]):
image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = imutils.resize(image, width=1150)
for j in range(image.shape[2]):
image[:,:,j] = image[:,:,j] * mask[:,:,i]
filename = "Output/segment_%d.jpg"%i
cv2.imwrite(filename,image)
i+=1
任何有关如何解决此问题的帮助将不胜感激,谢谢。
【问题讨论】:
【参考方案1】:我认为您需要在可视化 display_intance 中更改此行 line,并将 facecolor 从 none
更改为 None
。
我认为它正在创建 random 颜色,即使您没有明确指定它
【讨论】:
【参考方案2】:我发现了错误,正如在 Github 中向我建议的那样,我必须删除
`image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)`
行,因为我的图像已经转换为 RGB。
【讨论】:
以上是关于如何从 RCNN 中裁剪分割的对象?的主要内容,如果未能解决你的问题,请参考以下文章