2013-05-04 66 views
3

您好我是圖像分割的新手,我正在嘗試給定的代碼來獲取前景對象,但我得到像「不支持的格式或格式(僅8位,3通道的輸入圖像被支撐)在cvWatershed組合」不支持的格式或格式組合(僅支持8位,3通道輸入圖像)在cvWatershed

cv::Mat img0 = [img toMat]; 
cv::Mat img1; 
cv::cvtColor(img0, img0, CV_RGB2GRAY); 
cv::threshold(img0, img0, 100, 255, cv::THRESH_BINARY); 

cv::Mat fg; 
cv::erode(img0,fg,cv::Mat(),cv::Point(-1,-1),6); 

cv::Mat bg; 
cv::dilate(img0,bg,cv::Mat(),cv::Point(-1,-1),6); 
cv::threshold(bg,bg,1,128,cv::THRESH_BINARY_INV); 

cv::Mat markers(img0.size(),CV_8U,cv::Scalar(0)); 
markers= fg+bg; 

// CV :: namedWindow( 「標誌物」); // cv :: imshow(「Markers」,markers);

WatershedSegmenter segmenter; 
segmenter.setMarkers(markers); 
cv::Mat result1 = segmenter.process(img0); 

// cv :: Mat result1; result1.convertTo(result1,CV_8U);

UIImage * result = [UIImage imageWithMat:result1 andImageOrientation:[img imageOrientation]]; 
return result; 

,我嘗試通過調試和線路遇到錯誤提前

回答

2

cv::Mat result1 = segmenter.process(img0); 

感謝我再次分析我的代碼,並解決了這個問題。將圖像轉換爲ilpImage,然後使用代碼

WatershedSegmenter segmenter; 
segmenter.setMarkers(markers); 
markers=cvCreateImage(cvGetSize(my_iplImage), IPL_DEPTH_8U, 3); 
cv::Mat result1 = segmenter.process(markers); 
0

這提醒我的一個例子從書「opencv的2計算機視覺應用程序編程菜譜」其改變爲8位和3個信道的圖像。所有你應該做的是要做到這一點:

// Get the binary map 
    cv::Mat binary; 
    //binary = cv::imread("binary.bmp", 0); // prevent loading of pre-converted image 
    cvtColor(image, binary, CV_BGR2GRAY); // instead convert original 
    binary = binary < 65; // apply threshold 

整個代碼(不包括水分段報頭)將是這樣的:

#include <iostream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include "watershedSegmentation.h" 

int main() 
{ 
    // Read input image 
    cv::Mat image = cv::imread("group.jpg"); 
    if (!image.data) 
     return 0; 

    // Display the image 
    cv::namedWindow("Original Image"); 
    cv::imshow("Original Image", image); 

// // Get the binary map 
    cv::Mat binary; 
    //binary = cv::imread("binary.bmp", 0); // prevent loading of pre-converted image 
    cvtColor(image, binary, CV_BGR2GRAY); // instead convert original 
    binary = binary < 60; // apply threshold 

    // Display the binary image 
    cv::namedWindow("Binary Image"); 
    cv::imshow("Binary Image", binary); 

    // Eliminate noise and smaller objects 
    cv::Mat fg; 
    cv::erode(binary, fg, cv::Mat(), cv::Point(-1, -1), 6); 

    // Display the foreground image 
    cv::namedWindow("Foreground Image"); 
    cv::imshow("Foreground Image", fg); 

    // Identify image pixels without objects 
    cv::Mat bg; 
    cv::dilate(binary, bg, cv::Mat(), cv::Point(-1, -1), 6); 
    cv::threshold(bg, bg, 1, 128, cv::THRESH_BINARY_INV); 

    // Display the background image 
    cv::namedWindow("Background Image"); 
    cv::imshow("Background Image", bg); 

    // Show markers image 
    cv::Mat markers(binary.size(), CV_8U, cv::Scalar(0)); 
    markers = fg + bg; 
    cv::namedWindow("Markers"); 
    cv::imshow("Markers", markers); 

    // Create watershed segmentation object 
    WatershedSegmenter segmenter; 
    // Set markers and process 
    segmenter.setMarkers(markers); 
    segmenter.process(image); 

    // Display segmentation result 
    cv::namedWindow("Segmentation"); 
    cv::imshow("Segmentation", segmenter.getSegmentation()); 

    // Display watersheds 
    cv::namedWindow("Watersheds"); 
    cv::imshow("Watersheds", segmenter.getWatersheds()); 

    cv::waitKey(); 
    return 0; 
}