How to include batch processing with ( for loop ) for multiple image and different threshold based on the image

asked 2017-06-30 05:06:03 -0500

#include "stdafx.h"

include "opencv2\core\core.hpp"

include "opencv2\imgproc\imgproc.hpp"

include "opencv2\highgui\highgui.hpp"

include <math.h>

using namespace std; using namespace cv;

Mat ConverttoGrey(Mat RGBImg, int iHeight, int iWidth) { Mat GreyImg = Mat::zeros(RGBImg.size(), CV_8UC1); for (int i = 0; i < iHeight; i++) { for (int j = 0; j < iWidth * 3; j = j + 3) { GreyImg.at<uchar>(i, j / 3) = (RGBImg.at<uchar>(i, j) + RGBImg.at<uchar>(i, j + 1) + RGBImg.at<uchar>(i, j + 2)) / 3; } } return GreyImg; } Mat BinarizeImg(Mat GreyImg, int iHeight, int iWidth, int iThreshold) { Mat BinaryImg = Mat::zeros(GreyImg.size(), CV_8UC1); for (int i = 0; i < iHeight; i++) { for (int j = 0; j < iWidth; j++) { if (GreyImg.at<uchar>(i, j) > iThreshold) { BinaryImg.at<uchar>(i, j) = 255; } } } return BinaryImg; } Mat BinarizePlate(Mat GreyImg, int iHeight, int iWidth, int iThreshold) { Mat BinarizeImg = Mat::zeros(GreyImg.size(), CV_8UC1); for (int i = 0; i < iHeight; i++) { for (int j = 0; j < iWidth; j++) { if (GreyImg.at<uchar>(i, j) > iThreshold) { float AverageLeft = (GreyImg.at<uchar>(i - 1, j - 1) + GreyImg.at<uchar>(i, j - 1) + GreyImg.at<uchar>(i + 1, j - 1)) / 3; float AverageRight = (GreyImg.at<uchar>(i - 1, j + 1) + GreyImg.at<uchar>(i, j + 1) + GreyImg.at<uchar>(i + 1, j + 1)) / 3; if (abs(AverageLeft - AverageRight)>iThreshold) { BinarizeImg.at<uchar>(i, j) = 255; } } } } return BinarizeImg; }

Mat DilatedImage(Mat BinarizeImg, int DilationStep) {

Mat DilatedImg = Mat::zeros(BinarizeImg.size(), CV_8UC1);
for (int i = DilationStep; i < BinarizeImg.rows - DilationStep; i++)            //Define the location of first pixel,where to start
{
    for (int j = DilationStep; j < BinarizeImg.cols - DilationStep; j++)
    {
        for (int ii = -DilationStep; ii <= DilationStep; ii++)              //-1,0,1...,expand the mask and define the scope to find neighbour
        {
            for (int jj = -DilationStep; jj <= DilationStep; jj++)
            {
                if (BinarizeImg.at<uchar>(i + ii, j + jj) == 255)           //if any pixel in neighbour area is white then change to white,else maintain black color
                    DilatedImg.at<uchar>(i, j) = 255;
            }
        }

    }
}

return DilatedImg;

}

int otsuth(Mat GreyImg, int iHeight, int iWidth) {

float count[256] = { 0.0 };

for (int i = 0; i < GreyImg.rows; i++)
{
    for (int j = 0; j < GreyImg.cols; j++)
        count[GreyImg.at<uchar>(i, j)]++;

}

//calculate the probability of each intensity

float probability[256] = { 0.0 };

for (int k = 0; k < 256; k++)
    probability[k] = count[k] / (GreyImg.rows * GreyImg.cols);


//calculate the cimulative frequency

float Omega[256] = { 0.0 };

Omega[0] = probability[0];

for (int i = 1; i < 256; i++)
    Omega[i] = probability[i] + Omega[i - 1];

float meu[255] = { 0,0 };
meu[0] = 0;

for (int i = 1; i < 256; i++)
    meu[i] = i*probability[i] + meu[i - 1];

float sigma[255] = { 0,0 };

for (int i = 1; i < 256; i++)
    meu[i] = i*probability[i] + meu[i - 1];


int iMax = 0;
int iOtsu = 0;
for (int k = 1; k < 256; k++)
{
    sigma[k] = pow(meu[255] * Omega[k] - meu[k], 2) / (Omega[k] * (1 - Omega[k]));
    if (iMax < sigma[k])
    {
        iMax = sigma[k];
        iOtsu = k;
    }
}


return iOtsu;

}

int main ... (more)

edit retag flag offensive close merge delete

Comments

instead of throwing a wall of code at us, - try to cut it down to a small, reproducable example, also try to explain in words , where your actual problem is, please.

berak gravatar imageberak ( 2017-06-30 05:27:39 -0500 )edit

are you aware, that opencv has builtin functions for grayscale, binarizing, dilating, etc ?

berak gravatar imageberak ( 2017-06-30 06:06:42 -0500 )edit