【问题标题】:Counting Regions of Interest in a Picture [closed]计算图片中的感兴趣区域[关闭]
【发布时间】:2015-12-16 21:47:28
【问题描述】:

我需要计算下图中的条带数量(如标签所示):

我有数百张照片需要分析,我很好奇是否有一种方法可以自动隔离感兴趣的区域并对每张照片进行简单计数。我对图像分析几乎没有经验,任何让我开始的建议将不胜感激。

【问题讨论】:

  • 据我所知,图像识别是一个非常棘手的问题。要开始使用 SO 帮助解决它,您需要自己开始 - 发布一些您已经完成的代码,有些人会帮助您!
  • 有人(据说是你)几乎问了the same question on the ImageJ forum。最好将不同资源上的交叉帖子链接起来,这样其他人也可以找到可能只出现在其中一个地方的有价值的信息。
  • 对不起@JanEglinger,从现在开始我会养成这样的习惯。

标签: matlab opencv image-processing count imagej


【解决方案1】:

请运行我为您工作的以下代码。它大约足够接近并调整它。祝你好运..!

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include "tchar.h"
using namespace cv;
using namespace std;

#define INPUT_FILE              "u.jpg"
#define OUTPUT_FOLDER_PATH      string("")

int _tmain(int argc, _TCHAR* argv[])
{
    Mat large = imread(INPUT_FILE);
    Mat rgb;
    // downsample and use it for processing
    pyrDown(large, rgb);
    Mat small;
    cvtColor(rgb, small, CV_BGR2GRAY);
    // morphological gradient
    Mat grad;
    Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(2, 2));
    Mat morphKernel1 = getStructuringElement(MORPH_ELLIPSE, Size(1, 1));
    morphologyEx(small, grad, MORPH_GRADIENT, morphKernel);
    // binarize
    Mat bw;
    threshold(grad, bw, 5.0, 50.0, THRESH_BINARY | THRESH_OTSU);
    // connect horizontally oriented regions
    Mat connected;
    morphKernel = getStructuringElement(MORPH_RECT, Size(5, 1));
    morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);
    morphologyEx(bw, connected, MORPH_OPEN, morphKernel1);
    // find contours
    Mat mask = Mat::zeros(bw.size(), CV_8UC1);
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    // filter contours
    int y=0;
    for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])
    {
        Rect rect = boundingRect(contours[idx]);
        Mat maskROI(mask, rect);
        maskROI = Scalar(0, 0, 0);
        // fill the contour
        drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);

        double a=contourArea( contours[idx],false);

            if(a> 75)

        {
            rectangle(rgb, rect, Scalar(0, 255, 0), 2);
            y++;
        }
        imshow("Result1",rgb);
    }
    cout<<" The number of elements"<<y<< endl; 
    imshow("Result",mask);
    imwrite(OUTPUT_FOLDER_PATH + string("rgb.jpg"), rgb);
    waitKey(0);
    return 0;
}

【讨论】:

  • 感谢@Arjun 这么好的回复,这正是我想要做的。我现在将弄清楚如何对指定目录中的所有文件重复此操作并计算感兴趣的区域。干杯
  • 我很高兴它帮助了你@BillyBoy 祝你好运..!!
猜你喜欢
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 2012-02-22
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
相关资源
最近更新 更多