std::vector to cv::MatConcatenating two std::vectorsHow to find out if an item is present in a std::vector?How do I erase an element from std::vector<> by index?What is the easiest way to initialize a std::vector with hardcoded elements?Appending a vector to a vectorGet frame from video with libvlc smem and convert it to opencv Mat. (c++)Fastest way to apply color matrix to RGB image using OpenCV 3.0?Colorizing 16 bit Greyscale Mat produces only half of imageHow to reassign an individual element of a 2D parallel vector with a 1D vector?Convertion of Mat to 2d array and access the pixel values in c++

Don't understand MOSFET as amplifier

How to "know" if I have a passion?

How to persuade recruiters to send me the Job Description?

Why don't politicians push for fossil fuel reduction by pointing out their scarcity?

Why didn’t Doctor Strange stay in the original winning timeline?

How can I support the recycling, but not the new production of aluminum?

Does Git delete empty folders?

Is it safe to remove the bottom chords of a series of garage roof trusses?

Do I have to learn /o/ or /ɔ/ separately?

How can I use unicode in this condition?

How should I think about joining a company whose business I do not understand?

The sound of thunder's like a whip

Is there such a thing as too inconvenient?

Thread-safe, Convenient and Performant Random Number Generator

Efficiently pathfinding many flocking enemies around obstacles

How to look up identical column names in two dataframes and combine the matched columns

A second course in the representation theory

How to specify and fit a hybrid machine learning - linear model

Why doesn't mathematics collapse even though humans quite often make mistakes in their proofs?

Why my earth simulation is slower than the reality?

In an emergency, how do I find and share my position?

Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?

Most practical knots for hitching a line to an object while keeping the bitter end as tight as possible, without sag?

How much code would a codegolf golf if a codegolf could golf code?



std::vector to cv::Mat


Concatenating two std::vectorsHow to find out if an item is present in a std::vector?How do I erase an element from std::vector<> by index?What is the easiest way to initialize a std::vector with hardcoded elements?Appending a vector to a vectorGet frame from video with libvlc smem and convert it to opencv Mat. (c++)Fastest way to apply color matrix to RGB image using OpenCV 3.0?Colorizing 16 bit Greyscale Mat produces only half of imageHow to reassign an individual element of a 2D parallel vector with a 1D vector?Convertion of Mat to 2d array and access the pixel values in c++






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















what is the best way to convert a vector containing R,G and B color values in a cv::Vec3b vector to a cv::Mat? The position of the pixel does not matter, I just want to perform the cv::split operation and then put all three color levels to a histogramm.



I tried this, but it does not work as the split operation is returning a cv::Mat with only one plane of colors:



cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) 
std::vector<cv::Vec3b> ColorStack;
cv::Vec3b vec;
int nPixel = 0;

for (int y = 0; y < Gray.rows; y++)
for (int x = 0; x < Gray.cols; x++)
if (Gray.at<unsigned char>(y, x) == 255)
ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
nPixel++;




return cv::Mat(1, nPixel, CV_8UC3, &ColorStack[0]);



The strange thing is, that I really should have only yellow pixel but when printing the MAT object, I get all possible colors. The Col.at<cv::Vec3b>(y, x) returns the correct color but in RGB order - I thought, OpenCV is using BGR.



Thank you,
Jan










share|improve this question


























  • if that vector isn't changed anymore and doesn't go out of scope while you use the mat (before splitting), you can use the memory directly: cv::Mat testMat = cv:Mat(height, width, CV_8UC3, ColorStack.data()); then you can either split testMat or .clone it or whatever you like. Might be one copying of the data less than your approach, but will fail if the vector goes out of scope until you have done everything with testMat.

    – Micka
    Mar 27 at 16:07


















0















what is the best way to convert a vector containing R,G and B color values in a cv::Vec3b vector to a cv::Mat? The position of the pixel does not matter, I just want to perform the cv::split operation and then put all three color levels to a histogramm.



I tried this, but it does not work as the split operation is returning a cv::Mat with only one plane of colors:



cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) 
std::vector<cv::Vec3b> ColorStack;
cv::Vec3b vec;
int nPixel = 0;

for (int y = 0; y < Gray.rows; y++)
for (int x = 0; x < Gray.cols; x++)
if (Gray.at<unsigned char>(y, x) == 255)
ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
nPixel++;




return cv::Mat(1, nPixel, CV_8UC3, &ColorStack[0]);



The strange thing is, that I really should have only yellow pixel but when printing the MAT object, I get all possible colors. The Col.at<cv::Vec3b>(y, x) returns the correct color but in RGB order - I thought, OpenCV is using BGR.



Thank you,
Jan










share|improve this question


























  • if that vector isn't changed anymore and doesn't go out of scope while you use the mat (before splitting), you can use the memory directly: cv::Mat testMat = cv:Mat(height, width, CV_8UC3, ColorStack.data()); then you can either split testMat or .clone it or whatever you like. Might be one copying of the data less than your approach, but will fail if the vector goes out of scope until you have done everything with testMat.

    – Micka
    Mar 27 at 16:07














0












0








0








what is the best way to convert a vector containing R,G and B color values in a cv::Vec3b vector to a cv::Mat? The position of the pixel does not matter, I just want to perform the cv::split operation and then put all three color levels to a histogramm.



I tried this, but it does not work as the split operation is returning a cv::Mat with only one plane of colors:



cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) 
std::vector<cv::Vec3b> ColorStack;
cv::Vec3b vec;
int nPixel = 0;

for (int y = 0; y < Gray.rows; y++)
for (int x = 0; x < Gray.cols; x++)
if (Gray.at<unsigned char>(y, x) == 255)
ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
nPixel++;




return cv::Mat(1, nPixel, CV_8UC3, &ColorStack[0]);



The strange thing is, that I really should have only yellow pixel but when printing the MAT object, I get all possible colors. The Col.at<cv::Vec3b>(y, x) returns the correct color but in RGB order - I thought, OpenCV is using BGR.



Thank you,
Jan










share|improve this question
















what is the best way to convert a vector containing R,G and B color values in a cv::Vec3b vector to a cv::Mat? The position of the pixel does not matter, I just want to perform the cv::split operation and then put all three color levels to a histogramm.



I tried this, but it does not work as the split operation is returning a cv::Mat with only one plane of colors:



cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) 
std::vector<cv::Vec3b> ColorStack;
cv::Vec3b vec;
int nPixel = 0;

for (int y = 0; y < Gray.rows; y++)
for (int x = 0; x < Gray.cols; x++)
if (Gray.at<unsigned char>(y, x) == 255)
ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
nPixel++;




return cv::Mat(1, nPixel, CV_8UC3, &ColorStack[0]);



The strange thing is, that I really should have only yellow pixel but when printing the MAT object, I get all possible colors. The Col.at<cv::Vec3b>(y, x) returns the correct color but in RGB order - I thought, OpenCV is using BGR.



Thank you,
Jan







opencv vector colors mat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 15:49







Jan021981

















asked Mar 27 at 15:12









Jan021981Jan021981

12812 bronze badges




12812 bronze badges















  • if that vector isn't changed anymore and doesn't go out of scope while you use the mat (before splitting), you can use the memory directly: cv::Mat testMat = cv:Mat(height, width, CV_8UC3, ColorStack.data()); then you can either split testMat or .clone it or whatever you like. Might be one copying of the data less than your approach, but will fail if the vector goes out of scope until you have done everything with testMat.

    – Micka
    Mar 27 at 16:07


















  • if that vector isn't changed anymore and doesn't go out of scope while you use the mat (before splitting), you can use the memory directly: cv::Mat testMat = cv:Mat(height, width, CV_8UC3, ColorStack.data()); then you can either split testMat or .clone it or whatever you like. Might be one copying of the data less than your approach, but will fail if the vector goes out of scope until you have done everything with testMat.

    – Micka
    Mar 27 at 16:07

















if that vector isn't changed anymore and doesn't go out of scope while you use the mat (before splitting), you can use the memory directly: cv::Mat testMat = cv:Mat(height, width, CV_8UC3, ColorStack.data()); then you can either split testMat or .clone it or whatever you like. Might be one copying of the data less than your approach, but will fail if the vector goes out of scope until you have done everything with testMat.

– Micka
Mar 27 at 16:07






if that vector isn't changed anymore and doesn't go out of scope while you use the mat (before splitting), you can use the memory directly: cv::Mat testMat = cv:Mat(height, width, CV_8UC3, ColorStack.data()); then you can either split testMat or .clone it or whatever you like. Might be one copying of the data less than your approach, but will fail if the vector goes out of scope until you have done everything with testMat.

– Micka
Mar 27 at 16:07













1 Answer
1






active

oldest

votes


















0














Ok, the returning is wrong. This code works fine:



cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) 
std::vector<cv::Vec3b> ColorStack;
cv::Vec3b vec, vec2;

cv::imwrite("C:\HomeC\Test.bmp", Col);

for (int y = 0; y < Gray.rows; y++)
for (int x = 0; x < Gray.cols; x++)
if (Gray.at<unsigned char>(y, x) == 255)
//ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
vec = Col.at<cv::Vec3b>(y, x);
ColorStack.push_back(vec);




cv::Mat RetVal = cv::Mat(1, ColorStack.size(), CV_8UC3);
memcpy(RetVal.data, ColorStack.data(), ColorStack.size() * sizeof(cv::Vec3b));
return RetVal;






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380607%2fstdvectorcvvec3b-to-cvmat%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Ok, the returning is wrong. This code works fine:



    cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) 
    std::vector<cv::Vec3b> ColorStack;
    cv::Vec3b vec, vec2;

    cv::imwrite("C:\HomeC\Test.bmp", Col);

    for (int y = 0; y < Gray.rows; y++)
    for (int x = 0; x < Gray.cols; x++)
    if (Gray.at<unsigned char>(y, x) == 255)
    //ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
    vec = Col.at<cv::Vec3b>(y, x);
    ColorStack.push_back(vec);




    cv::Mat RetVal = cv::Mat(1, ColorStack.size(), CV_8UC3);
    memcpy(RetVal.data, ColorStack.data(), ColorStack.size() * sizeof(cv::Vec3b));
    return RetVal;






    share|improve this answer





























      0














      Ok, the returning is wrong. This code works fine:



      cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) 
      std::vector<cv::Vec3b> ColorStack;
      cv::Vec3b vec, vec2;

      cv::imwrite("C:\HomeC\Test.bmp", Col);

      for (int y = 0; y < Gray.rows; y++)
      for (int x = 0; x < Gray.cols; x++)
      if (Gray.at<unsigned char>(y, x) == 255)
      //ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
      vec = Col.at<cv::Vec3b>(y, x);
      ColorStack.push_back(vec);




      cv::Mat RetVal = cv::Mat(1, ColorStack.size(), CV_8UC3);
      memcpy(RetVal.data, ColorStack.data(), ColorStack.size() * sizeof(cv::Vec3b));
      return RetVal;






      share|improve this answer



























        0












        0








        0







        Ok, the returning is wrong. This code works fine:



        cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) 
        std::vector<cv::Vec3b> ColorStack;
        cv::Vec3b vec, vec2;

        cv::imwrite("C:\HomeC\Test.bmp", Col);

        for (int y = 0; y < Gray.rows; y++)
        for (int x = 0; x < Gray.cols; x++)
        if (Gray.at<unsigned char>(y, x) == 255)
        //ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
        vec = Col.at<cv::Vec3b>(y, x);
        ColorStack.push_back(vec);




        cv::Mat RetVal = cv::Mat(1, ColorStack.size(), CV_8UC3);
        memcpy(RetVal.data, ColorStack.data(), ColorStack.size() * sizeof(cv::Vec3b));
        return RetVal;






        share|improve this answer













        Ok, the returning is wrong. This code works fine:



        cv::Mat GetOnlyColoredPixel(cv::Mat& Gray, cv::Mat& Col) 
        std::vector<cv::Vec3b> ColorStack;
        cv::Vec3b vec, vec2;

        cv::imwrite("C:\HomeC\Test.bmp", Col);

        for (int y = 0; y < Gray.rows; y++)
        for (int x = 0; x < Gray.cols; x++)
        if (Gray.at<unsigned char>(y, x) == 255)
        //ColorStack.push_back(Col.at<cv::Vec3b>(y, x));
        vec = Col.at<cv::Vec3b>(y, x);
        ColorStack.push_back(vec);




        cv::Mat RetVal = cv::Mat(1, ColorStack.size(), CV_8UC3);
        memcpy(RetVal.data, ColorStack.data(), ColorStack.size() * sizeof(cv::Vec3b));
        return RetVal;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 16:03









        Jan021981Jan021981

        12812 bronze badges




        12812 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380607%2fstdvectorcvvec3b-to-cvmat%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

            밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

            1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴