Very interesting:D
A really simple solution would be to binarize your image and to define a line in the upper area of the image and get the gray values. Then you could count the intersections. Here is an example:
The fancy algorithms arent always the best solution. Define what you want to do and try to find a solution as simple as possible.
Here is my solution in C++. Quick and dirty ;)
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <vector>
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
using namespace std;
Mat src, binary, morph, gray, morph_rgb;
/**
*Compute number of tabs
*/
int getNumberOfTabs(Mat& src, int start_col, int stop_col, int search_row, bool draw=false);
/**
* @function main
*/
int main(int argc, char** argv)
{
const int morph_size = 2;
const int start_col = 5;
const int stop_col = 1750;
const int row_index = 2;
/// Load an image
src = imread("C:\\Users\\phili\\Pictures\\Tab.png", 1);
//Convert for binarization
cvtColor(src, gray, CV_RGB2GRAY);
threshold(gray, binary, 164, 255, 1);
//Remove icons and text on tabs
Mat element = getStructuringElement(CV_SHAPE_RECT, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));
morphologyEx(binary, morph, CV_MOP_OPEN, element);
int nmb_tabs = getNumberOfTabs(morph, start_col, stop_col, row_index, true);
imshow("Original", src);
imshow("Binary", binary);
imshow("Binary", morph);
/// Wait until user finishes program
while (true)
{
int c;
c = waitKey(20);
if ((char)c == 27)
{
break;
}
}
}
int getNumberOfTabs(Mat& src,int start_col,int stop_col, int row_index, bool draw)
{
int length = stop_col - start_col;
//Extract gray value profil
Mat profil = cv::Mat(0, length, CV_8U);
profil = src.colRange(start_col, stop_col).row(row_index);
Mat src_rgb;
if (draw)
{
cvtColor(src, src_rgb, CV_GRAY2RGB);
line(src_rgb, Point(start_col, row_index), Point(stop_col, row_index), Scalar(0, 0, 255), 2);
}
//Convolve profil with [1 -1] to detect edges
unsigned char* input = (unsigned char*)(profil.data);
vector<int> positions;
for (int i = 0; i < stop_col - start_col - 1; i++)
{
//Kernel
int first = input[i];
int second = input[i + 1];
int ans = first - second;
//Positiv or negativ slope ?
if (ans < 0)
{
positions.push_back(i + 1);
if(draw)
circle(src_rgb, Point(i + start_col, row_index), 2, Scalar(255,0, 0), -1);
}
else if (ans > 0)
{
positions.push_back(i);
if (draw)
circle(src_rgb, Point(i + start_col + 1, row_index), 2, Scalar(255,0, 0), -1);
}
}
if (draw)
imshow("Detected Edges", src_rgb);
//Number of tabs
return positions.size() / 2;
}
and the results with the search line in red and the detected edges in blue: