Monday 24 February 2014

OpenCV Cpp: Crop region of interest using sliding window

#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
Point point1, point2; /* vertical points of the bounding box */
int drag = 0;
Rect rect; /* bounding box */
Mat img, roiImg; /* roiImg - the part of the image in the bounding box */
int select_flag = 0;
void mouseHandler(int event, int x, int y, int flags, void* param)
{
    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        /* left button clicked. ROI selection begins */
        point1 = Point(x, y);
        drag = 1;
    }
    if (event == CV_EVENT_MOUSEMOVE && drag)
    {
        /* mouse dragged. ROI being selected */
        Mat img1 = img.clone();
        point2 = Point(x, y);
        rectangle(img1, point1, point2, CV_RGB(255, 0, 0), 3, 8, 0);
        imshow("image", img1);
    }
    if (event == CV_EVENT_LBUTTONUP && drag)
    {
        point2 = Point(x, y);
        rect = Rect(point1.x,point1.y,x-point1.x,y-point1.y);
        drag = 0;
        roiImg = img(rect);
    }
    if (event == CV_EVENT_LBUTTONUP)
    {
        /* ROI selected */
        select_flag = 1;
        drag = 0;
    }
}
int main()
{
    int k;
    img=imread("roi.jpg");
    imshow("image", img);
    while(1)
    {
        cvSetMouseCallback("image", mouseHandler, NULL);
        if (select_flag == 1)
        {
            imshow("ROI", roiImg); /* show the image bounded by the box */
            imwrite("Roi_image.jpg",roiImg);
        }
        rectangle(img, rect,0, 3, 8, 0);
        imshow("image", img);
        k = waitKey(10);
        if (k==27)
        {
            break;
        }
    }
    return 0;
}

Friday 14 February 2014

Opencv CPP program for converting to grey, resizing image and edge detection

#include "opencv\cv.h"
#include "opencv\highgui.h"
#include <iostream>

using namespace cv;
using namespace std;
#define fname "image/b.jpg"

void grey();
int resize(int ht, int wt);
int edge1();
static void onTrackbar(int, void*);

int main(int argc, char** argv)
{
cout<<"Image Location:"<<fname;
IplImage* img = cvLoadImage(fname);
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
cout<<"\nGrey conversion..";
grey();
cvWaitKey(0);
cout<<"\n\nEnter pizel size for resize:";
int ht,wt;
cin>>ht;
cin>>wt;
resize(ht,wt);
cvWaitKey(0);
edge1();
cout<<"image saved successfully";
return 0;
}

void grey()
{

    char * file = fname;
    IplImage * img2 = cvLoadImage(file, CV_LOAD_IMAGE_GRAYSCALE);
    cvSaveImage("output/grey.jpg", img2);
    cvNamedWindow( "Grey", CV_WINDOW_AUTOSIZE );
    cvShowImage("Grey", img2);
    cvWaitKey(0);
    cvDestroyWindow("Grey");
    cvReleaseImage(&img2);
}

int resize(int ht, int wt)
{
    IplImage* inputImage = cvLoadImage("output/grey.jpg");
    IplImage* resizedImageLinear =cvCreateImage(cvSize(ht,wt),inputImage->depth, inputImage->nChannels);
    cvResize(inputImage, resizedImageLinear, CV_INTER_LINEAR);
    cvSaveImage("resize/resized.jpg", resizedImageLinear);
    cvNamedWindow("Linear");
    cvShowImage("Linear", resizedImageLinear);
    cvWaitKey(0);
    cvDestroyWindow("Linear");
    cvReleaseImage(&resizedImageLinear);
    return 0;
}

int edgeThresh = 1;
Mat image, gray, edge, cedge;

static void onTrackbar(int, void*)
{
    blur(gray, edge, Size(3,3));
    Canny(edge, edge, edgeThresh, edgeThresh*3, 3);
    cedge = Scalar::all(0);

    image.copyTo(cedge, edge);
    imshow("Edge map", cedge);
    imwrite("edge.jpg",cedge);
}


int edge1()
{
    image = imread(fname, 1);
    if(image.empty())
    {
        printf("Cannot read image file");
        return -1;
    }
    cedge.create(image.size(), image.type());
    cvtColor(image, gray, CV_BGR2GRAY);
    namedWindow("Edge map", 1);
    createTrackbar("Canny threshold", "Edge map", &edgeThresh, 100, onTrackbar);
    onTrackbar(0, 0);
    waitKey(0);
    return 0;
}

Opencv CPP function to resize image

#include "opencv\cv.h"
#include "opencv\highgui.h"

int resize(IplImage* inputImage)
{
    // Load input image
    //IplImage* inputImage = cvLoadImage("b.jpg");

    // Create images for resize
    IplImage* resizedImageLinear =cvCreateImage(cvSize(220,220),inputImage->depth, inputImage->nChannels);
   

    // Resize input image using different method
    // Bilinear interpolation
    cvResize(inputImage, resizedImageLinear, CV_INTER_LINEAR);
    // Nearest neighbor
   
    // Save images
    cvSaveImage("b_resized.jpg", resizedImageLinear);
  
    // Create windows to show images
    cvNamedWindow("Input");
    cvNamedWindow("Linear");
   

    // Show images
    cvShowImage("Input", inputImage);
    cvShowImage("Linear", resizedImageLinear);
   
    // Wait for user input
    cvWaitKey(0);

    // Destroy windows
    cvDestroyWindow("Input");
    cvDestroyWindow("Linear");
  

    // Release memory
    cvReleaseImage(&inputImage);
    cvReleaseImage(&resizedImageLinear);
   

    // Exit
    return 0;
}

OpenCV Program: Image Loading

Save image at location where your program is saved.
change the name of the image.

#include "opencv\cv.h"
#include "opencv\highgui.h"
#include <iostream>
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage( "Brain.png" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
return 0;
}

Opencv Cpp Program to convert color image to grey scale

#include "opencv\cv.h"
#include "opencv\highgui.h"
#define fname "image/baboon.jpg"
int main(int argc, char** argv())
{

    char * file = fname;
    IplImage * img2 = cvLoadImage(file, CV_LOAD_IMAGE_GRAYSCALE);
    cvSaveImage("output/grey.jpg", img2);
    cvNamedWindow( "Grey", CV_WINDOW_AUTOSIZE );
    cvShowImage("Grey", img2);
    cvWaitKey(0);
    cvDestroyWindow("Grey");
    cvReleaseImage(&img2);
    return 0;
}

Brain tumor detection & classification using OpenCV

Title: Brain tumor detection & classification using OpenCV

Abstract :

In this project we are going to apply modified image segmentation technique on MRI scan images in order to detect brain tumors. Also a modified Probabilistic Neural Network (PNN) model will use for automated brain tumor classification using MRI scans. Present available tool is able to detect the brain tumor only but it is not able to classify brain tumor. Present tool is available in MATLAB which is too costly. We are creating the tool using OpenCV which is open source and able to classify the type of brain Tumor. This tool will helpful for doctors for automatic detection & classification of brain tumor.

Introduction:

Brain tumor detection and classification system detects brain tumor and classifies it. Brain tumor has two types i.e. benign and malignant tumor. Tumor is mass of tissue that serves for no purpose and generally exists at expense of healthy tissue. Benign brain tumors, composed of harmless cells, have clearly defined borders, can usually be completely removed, and are unlikely to recur. A benign tumor is basically a tumor that doesn't revert and doesn't spread to other parts of the body. Benign tumors tend to grow more slowly than malignant tumors and are less likely to cause health problems. But malignant brain tumors do not have distinct borders. They tend to grow rapidly, increasing pressure within the brain and can spread in the brain or spinal cord beyond the point where they originate. They grow faster than benign tumors and are more likely to cause health problems. The Brain tumor detection and classification system will take MRI scan image and compare it with anatomical structure of healthy brain. After that smoothing of image is done and Region of interest (ROI) is determined. From ROI we can classify brain tumor using number of data sets stored in system.

Proposed work:

In our proposed work we will take MRI scanned image and apply following steps on it for detection and classification of brain tumor.
Brain tumor identification and classification system contains following steps:

A) Image Acquisition:

In our proposed approach we first will consider that the MRI scan images of a given patient are either color, Gray-scale or intensity images herein are displayed with a default size of 220×220. If it is color image, a Gray-scale converted image is defined by using a large matrix whose entries are numerical values between 0 and 255, where 0 corresponds to black and 255 to white for instance. Then the brain tumor detection of a given patient consist of two main stages namely, image segmentation and edge detection.

B) Image Segmentation:

The objective of image segmentation is to cluster pixels into image region. The segmentation is useful for identifying region of interest i.e. locate tumor and other abnormalities. The proposed system is based on information about anatomical structure of healthy parts and compares it with healthy parts. The comparison done with reference image of normal candidate brain scan image as shown in fig.1. After comparison it will locate abnormal parts of brain tumor patient. The abnormal brain is as shown in fig.2.

        
        Figure 1: Normal Brain                     Figure 2: Abnormal Brain

C) Smoothing of image:

There are different types of noise encountered by different techniques, depending on
noise nature and characteristics namely Gaussian noise and impulse noise. We will use
smoothing image filters for reducing Gaussian noise from MRI images & sharpening filters for
highlighting edges in an image. It was observed that smoothing and sharpening filter does not
remove noise completely from original image. Filtering is as shown in fig.3 and fig 4.

          
 Figure 3. Applying Gaussian filter           Figure 4: applying average filter

D) Edge detection:

Edge is the property attached to an individual pixel. The purpose of edge detection is
to finding Region of Interest. While preserving structural properties to be used for further image
processing. We will apply edge detection algorithm and calculate region of interest as shown
in fig 6. Our region of interest is tumor i.e. abnormal part present on brain. The white portion
shown in fig.6.is the tumor, since our focus is on this portion it will helpful to significantly
reduce amount of data in an image. After identifying tumor we will apply Canny Edge
Detection algorithm in order to classify brain tumor.

          
     Figure 6: Region of Interest                Figure 7: Canny Edge Detection

E) Methodology

Figure 8. Illustrate the sequence of proposed approach.

   

In proposed work we will take MRI scanned image. It can be in the form color or gray, if it is not in gray color the system will convert image into gray format. This gray image is given to the image segmentation. Segmented image is compared with stored data sets. After comparing we can detect brain tumor. For classification we will give this compared image to the neural network. By using probabilistic classification we can classify the tumor is in normal state or abnormal state.


Literature, reviews or other schemas:

[1] Dina Aboul Dahab1, Samy S. A. Ghoniemy2, Gamal M. Selim3
Dep. of Computer Engineering, Arab Academy for Science, Technology & Maritime Transport
Cairo, Egypt “Automated Brain Tumor Detection and Identification Using Image Processing and Probabilistic Neural Network Techniques”.
[2] Automatic Detection of Brain Abnormalities and Tumor Segmentation in MRI Sequence.
[3] International Journal of Image Processing and Visual Communication ISSN 2319-1724: Volume (Online) 1, Issue 2, October 2012.

OpenCV Cpp: Crop region of interest using sliding window

#include<opencv2/highgui/highgui.hpp> using namespace cv; using namespace std; Point point1, point2; /* vertical points of the boundi...