Ask Your Question
1

I want to know where exactly the source code cvtColor functions implemented?

asked 2017-10-09 10:46:53 -0600

Ajay gravatar image

updated 2018-01-08 05:46:25 -0600

I am studying Computer Vision, one of the assignment professor asked me to write your own function to convert the rgb to grayscale. Now I know how to do it in python using opencv. Can you please point out the implementation files for cvtColor in opencv 3.0 version. so that I can take look at how its implemented.

edit retag flag offensive close merge delete

Comments

opencv is an opensource lib. all codes is available on github.

LBerger gravatar imageLBerger ( 2017-10-09 11:08:29 -0600 )edit
2

{google cvtColor source code} -- the first link is the good

carton99 gravatar imagecarton99 ( 2017-10-09 11:10:50 -0600 )edit

https://github.com/opencv/opencv/blob... - 11,000 lines of code :) thank you.

Ajay gravatar imageAjay ( 2017-10-09 11:15:22 -0600 )edit
2

lol, yea, what did you expect ?

i guess you rather want to look at the formulas , not so much at the optimised c++ implementation ;)

berak gravatar imageberak ( 2017-10-09 11:18:42 -0600 )edit
2

:) :) @berak thank you so much. That doc helping a lot.

Ajay gravatar imageAjay ( 2017-10-09 11:20:33 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
4

answered 2017-10-09 12:08:01 -0600

a sample C++ code maybe helpful ( i modified akarsakov's code from SO)

#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>


void RGB2Gray(const cv::Mat& src, cv::Mat& dst)
{
    CV_Assert(src.type() == CV_8UC3);
    int rows = src.rows, cols = src.cols;

    dst.create(src.size(), CV_8UC1);

    if (src.isContinuous() && dst.isContinuous())
    {
        cols = rows * cols;
        rows = 1;
    }

    for (int row = 0; row < rows; row++)
    {
        const uchar* src_ptr = src.ptr<uchar>(row);
        uchar* dst_ptr = dst.ptr<uchar>(row);

        for (int col = 0; col < cols; col++)
        {
            dst_ptr[col] = (uchar)(src_ptr[0] * 0.114f + src_ptr[1] * 0.587f + src_ptr[2] * 0.299f);
            src_ptr += 3;
        }
    }
}

int main()
{
    cv::Mat SrcImg = cv::imread("../data/lena.jpg");
    cv::Mat DstImg;
    RGB2Gray(SrcImg, DstImg);
    imshow("gray", DstImg);
    cv::waitKey();
    return 0;
}
edit flag offensive delete link more

Comments

Thank you, I wish I have points to upvote this answer.

Ajay gravatar imageAjay ( 2017-10-09 12:09:41 -0600 )edit
2

@berak do you know why imshow("gray", DstImg); works here without cv:: prefix ?

sturkmen gravatar imagesturkmen ( 2017-10-09 13:50:10 -0600 )edit
2

^^ good one !

(we should look for "using namespace cv;" in some public api header)

berak gravatar imageberak ( 2017-10-09 14:10:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-09 10:46:22 -0600

Seen: 4,685 times

Last updated: Oct 09 '17