LoginSignup
yutapuu
@yutapuu

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

二値化画像の輪郭抽出について

解決したいこと

画像の輪郭抽出を行いたいです。これで合っていると思うのですがエラーが出ます。どこを修正したらよいのでしょうか?
ちなみにHLS2.jpgは以下の画像です
HLS2.jpg
輪郭抽出した後にボックスで囲むやつをやろうとしています。

該当するソースコード

import cv2
import numpy as np
import os

os.chdir("C:\\suzuki")
img = cv2.imread("HLS2.jpg", 1)



labels, contours, hierarchy = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

for i in range(0, len(contours)):
    if len(contours[i]) > 0:


        if cv2.contourArea(contours[i]) < 500:
            continue

        rect = contours[i]
        x, y, w, h = cv2.boundingRect(rect)
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 10)


cv2.imwrite('tulips_boundingbox.jpg', img)

エラーコード
error Traceback (most recent call last)
in
8
9
---> 10 labels, contours, hierarchy = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
11
12 for i in range(0, len(contours)):

error: OpenCV(3.4.2) C:\Miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\imgproc\src\contours.cpp:199: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'

0

2Answer

python は詳しくないのですが

FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL

とのことなので CV_8UC1 images つまりグレースケールな画像として読み込むことで解決できそうです。
フルカラー画像は好きな色が使えるので「白黒だけのフルカラー画像」が存在します。

もちろん mode=RETR_FLOODFILL でもエラーは消せると思いますが
上記の方法を試したあとに出力を比較する方が良いと思います。

人間と違って python はあなたを見捨てずにきちんと問題点をエラーとして指摘してくれます。
内容を無視するのはかわいそうです…。

4

Your answer might help someone💌