Ask Your Question
1

Pass a value say "the final value" from a main function to some other class I am designing.

asked 2014-02-27 04:05:26 -0600

srig gravatar image

Hi,

Actually I designed a code in C++ with the help of OpenCV to process some image. In the main function I did something on image img and the processed image which I get at last is img_final.

No I am making one new code in the same project and started with the class because its not possible to have two main functions in same project. So, I designed another alogrithm with the help of class. Now I want to get the values of img_final and some other 2-3 parameters that are to be used in the present code. Both the codes are in the same project.

Please help me how to do it. I am new to programming.

Thanks.

edit retag flag offensive close merge delete

Comments

2

Just create a class and declare all the parameters you are going to play with as public variables.Then in the same class add your required functions and add an extra function that fetches or fills up these parameters.So,there is a main,a class with the parameters and functions. Eg. 1)you read an image in main and store it in a matrix,also you determine the size and store it in a variable. 2)Now you define a class with variables as one matrix and one int,but keep them public or static private. 3)you define a function which gets data from main and store it in the class variables. 4)Now,you can use the class variables in different functions and also in main again

Hope this helps you:)

Abhishek Kumar Annamraju gravatar imageAbhishek Kumar Annamraju ( 2014-02-27 05:29:35 -0600 )edit

But how to define a function which gets data from main and store it in the class variables. This is the main problem actually. I have designed the class other than main function. I have also made a function in this class which will be doing some processing. But in that function only I need to use the final result of the main function. And I am facing problem in getting that final result of main function in the function of the class.

srig gravatar imagesrig ( 2014-02-28 00:05:27 -0600 )edit

Suppose this is my code of main function:

int main(int argc,char*argv[]) {

m.at<float>(i,j) = exp(-RR/(2b2))(1-exp(-S2/(2c2)))*exp(-hypotenuse/gradscale); return 0; }

m is the matrix which is the final result this function... and shaprDetect() is the function of class Vsharp. now I want to use the "m" in this function shaprDetect to do some processing on it.

void Vsharp::shaprDetect() { }

Now tell me how to use that "m" in this function. }

srig gravatar imagesrig ( 2014-02-28 00:26:24 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-02-28 02:18:03 -0600

In the following code 1)I have defined two classes one to store an image in it and another class which gets that image from first class for further manipulation. 2)Also I have fetched some image properties in main,passed them to class store and used them in class grayimage. So,any changes in main wont directly affect the images or data in classes.To make changes in them you need to call them(classes and its objects) personally.

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

class store              
{
    public:             //Keep it public so that it can be used in other classes
        Mat image;
        int blue;
        int green;
        int red;



    public: 
    void getimage(Mat src) //storing image into a class variable named image
    {               

        if(! src.data )                              
        {
            cout <<  "src not filled" << endl ;
        }

        else
        {
            image = src.clone();
            cout << "got image" <<endl;
        }
    }

    void getdata(int bb,int gg,int rr)//storing data into class variables
    {
        blue =bb;
        green= gg;
        red=rr;
    }

    void showimage()//Display stored image using a class function
    {       
        if(! image.data )                              
        {
            cout <<  "Image not passed into this function" << std::endl ;

        }

        else
        {

            namedWindow( "Display window", WINDOW_AUTOSIZE );
            imshow( "Display window", image );
            cout << "image ready to get displayed" << endl;
        }   
    }

};  //first class ends here;

class grayimage   //creating a class where i need to manipulate images and data obtained from another class
{

    private:
        Mat gray_image;
        int btrans,gtrans,rtrans;

    public:
        void setgray(Mat src) //converting the image to grayscale
        {
            if(! src.data )                              
            {
                cout <<  "src in second class not filled" << endl ;
            }

            else
            {
                cvtColor(src, gray_image, CV_BGR2GRAY );
                cout << "image conversion done" <<endl;
            }

        }

        void transferdata(int b,int g,int r)  //creating a transfer function
        {
            btrans = b;
            gtrans = g;
            rtrans = r;
        }       


        void showdata()
        {
            cout << "values of b,g,r before gray scale coversion are" << btrans << "," << gtrans << "," << rtrans << "," <<endl; 
        }

        void displaygray()
        {
            if(! gray_image.data )                              
            {
                cout <<  "Image not passed into this function" << std::endl ;

            }

            else
            {

                namedWindow( "gray image", WINDOW_AUTOSIZE );
                imshow( "gray image", gray_image );
                cout << "gray image ready to get displayed" << endl;
            }   
        }

};  

//main starts here

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat img;
    img = imread(argv[1], CV_LOAD_IMAGE_COLOR);    

    if(! img.data )                              
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    unsigned char *input = (unsigned char*)(img.data);
    int i,j,r,g,b;
    for(int i = 0;i < img.rows;i++)
    {
        for(int j = 0;j < img.cols;j++)
        {
                b = input[img.step * j + i ] ;
                g = input[img.step * j + i + 1];
                r = input[img.step * j + i + 2];
            }
    }

    store image1; //object to store image created
    image1.getimage(img);//image from main stored in object
    image1.getdata(b,g,r);//data from main stored in object
    image1.showimage();//displaying the stored image 

    grayimage image2;   //creating a manipulator object
    image2.setgray(image1.image); //fetches image from the first object that was stored and converts it into grayscale 
    image2.transferdata(image1.blue,image1.green,image1.red);//fetches data from the first ...
(more)
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-02-27 04:05:26 -0600

Seen: 364 times

Last updated: Feb 28 '14