Ask Your Question

Darth Revan's profile - activity

2020-01-30 05:53:43 -0500 commented answer Unexpected Results at Subtracting Two Images

CHECKMATE! Now we are on the same side.

2020-01-29 14:16:38 -0500 commented answer Unexpected Results at Subtracting Two Images

I understand your point, I try to re-write built-in OpenCV functions which are already benefit from parallelization and

2020-01-23 01:40:20 -0500 commented answer Unexpected Results at Subtracting Two Images

Okay, I marked this as "solution". Case closed but I still wonder that why did I get a down vote lol.

2020-01-23 01:39:02 -0500 marked best answer Unexpected Results at Subtracting Two Images

Hello there,

I try to implement subtraction of two images. I am sure there are too many people who are ready to shot me with their "Why don't you just use cv::subtract or simply cv::Mat Result = Image1 - Image2;" gun. Jokes aside, the reason is that I try to implement my own version to use it as a function which is going to run on separate threads. When I used

    #include <opencv2/opencv.hpp>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <cstdio>
    #include <new>
    #include <chrono>
    #include <omp.h>

void rgbToHsv(cv::Mat* inputImage, cv::Mat* outputImage);
void subtractImage(cv::Mat* inputImage, cv::Mat* outputImage, cv::Mat* imageMask);

int main(int argc, char const *argv[])
{
  cv::Mat inputImage = imread("lena.png", cv::IMREAD_UNCHANGED); //Input image

  //Converting image from RGB to HSV colorspace
  cv::Mat inputImageHsv = inputImage.clone();
  rgbToHsv(&inputImage, &inputImageHsv);

//Splitting V channel for later use
  cv::Mat inputImageHsvChannels[3];
  cv::split(inputImageHsv, inputImageHsvChannels);

  cv::Mat inputImageH = inputImageHsvChannels[0];
  cv::Mat inputImageS = inputImageHsvChannels[1];
  cv::Mat inputImageV = inputImageHsvChannels[2];

  cv::Mat blurredImage = inputImageV.clone();
  cv::Mat imageMask = inputImageV.clone();
  cv::Mat imageMask2 = inputImageV.clone();
  cv::Mat imageMask3 = inputImageV.clone();

  cv::GaussianBlur(inputImageV, blurredImage, cv::Size(5,5), 0, 0);


  cv::subtract(inputImageV,blurredImage, imageMask);
  imageMask2 = inputImageV - blurredImage;  
  subtractImage(&inputImageV, &blurredImage, &imageMask3);

  cv::imshow("Subtracted Image1 OPENCV", imageMask);
  cv::imshow("Subtracted Image2 OPENCV", imageMask2);
  cv::imshow("Subtracted Image3 MY FUNC", imageMask3);
  cv::waitKey();

  return 0;
}

void subtractImage(cv::Mat* inputImage1, cv::Mat* inputImage2, cv::Mat* outputImage)
{
  for(int i = 0; i < inputImage1->rows; ++i)
  {
    for(int j = 0; j < inputImage1->cols; ++j)
    {
      int iVal = inputImage1->at<uchar>(i,j) - inputImage2->at<uchar>(i,j);

      if(iVal < 0)
        outputImage->at<uchar>(i,j) = 0;

      outputImage->at<uchar>(i,j) = iVal;
    }
  }
}

void rgbToHsv(cv::Mat* inputImage, cv::Mat* outputImage)
{
  double redSc = 0, greenSc = 0, blueSc = 0; //Scaled R, G, B values of current pixel
  double h = 0, s = 0, v = 0; //R, G, B values of current pixel
  double cmin = 0, cmax = 0; //Min and max dummy variables
  double delta = 0; //Difference between min and max

  int channels = inputImage->channels();
  int nRows = inputImage->rows;
  int nCols = inputImage->cols*channels;

  if (inputImage->isContinuous())
  {
    nCols *= nRows;
    nRows = 1;
  }

  uchar* p;
  uchar* q;

  for(int i = 0; i < nRows; ++i){
    p = inputImage->ptr<uchar>(i);
    q = outputImage->ptr<uchar>(i);

    for(int j = 0; j < nCols; j+=3){    
      redSc = p[j+2] / 255.;
      greenSc = p[j+1] / 255.;
      blueSc = p[j] / 255.;

      cmin = std::min(std::min(redSc, greenSc), blueSc);
      cmax = std::max(std::max(redSc, greenSc), blueSc);
      delta = cmax - cmin;

      if(!delta){
        h = 0.;
        s = 0.;
        v = cmax * 255.;
      }
      else{
        if(cmax == redSc)
        h = 60. * ((greenSc - blueSc)/delta);

        if(cmax == greenSc)
        h = 120 + (60. * (((blueSc - redSc)/delta)));

        if(cmax == blueSc)
        h = 240 + (60. * (((redSc - greenSc)/delta)));

        if(h < 0)
        h += 360;

        h = (h/2);

        v = cmax* 255.;

        s = ((cmax==0)?0:((delta/cmax)*255.));

        q[j+2] = v;   //Red
        q[j+1] = s; //Green
        q[j] = h; //Blue   
      }
    }
  }
}

Here are the results.

OpenCV Style 1 OpenCV Style 2 My Implementation

2020-01-21 11:47:01 -0500 commented answer Unexpected Results at Subtracting Two Images

saturate_cast worked like a charm and also I noticed that I forgot to add else{...}. Tetragramm would you please edit yo

2020-01-18 23:52:10 -0500 commented answer Unexpected Results at Subtracting Two Images

Yes I exactly did this one.

2020-01-18 03:24:20 -0500 commented answer Unexpected Results at Subtracting Two Images

Actually before creating this thread I also tried to cast inputImage1->at<uchar>(i,j) and inputImage2->at<

2020-01-17 15:24:46 -0500 commented question Unexpected Results at Subtracting Two Images

In my custom function void subtractImage(cv::Mat inputImage, cv::Mat outputImage, cv::Mat imageMask); I simply access e

2020-01-17 15:15:27 -0500 commented question Unexpected Results at Subtracting Two Images

Oh many thanks for the knowledge. I didn't know that cv::Mat is already a "smart pointer", so I used cv::Mat* to reduce

2020-01-17 15:03:56 -0500 edited question Unexpected Results at Subtracting Two Images

Unexpected Results at Subtracting Two Images Hello there, I try to implement subtraction of two images. I am sure there

2020-01-17 15:03:07 -0500 edited question Unexpected Results at Subtracting Two Images

Unexpected Results at Subtracting Two Images Hello there, I try to implement subtraction of two images. I am sure there

2020-01-17 15:02:38 -0500 edited question Unexpected Results at Subtracting Two Images

Unexpected Results at Subtracting Two Images Hello there, I try to implement subtraction of two images. I am sure there

2020-01-17 15:01:01 -0500 asked a question Unexpected Results at Subtracting Two Images

Unexpected Results at Subtracting Two Images Hello there, I try to implement subtraction of two images. I am sure there

2020-01-17 15:00:56 -0500 asked a question Unexpected Results at Subtracting Two Images

Unexpected Results at Subtracting Two Images Hello there, I try to implement subtraction of two images. I am sure there

2020-01-17 15:00:55 -0500 asked a question Unexpected Results at Subtracting Two Images

Unexpected Results at Subtracting Two Images Hello there, I try to implement subtraction of two images. I am sure there

2019-12-19 11:20:33 -0500 received badge  Notable Question (source)
2019-03-08 02:27:04 -0500 marked best answer CUDA Canny Edge Detector is slower than cv::Canny

Hello there.

This is my first post here. I started to learn to use OpenCV and its CUDA capabilities. I've written a simple code that reads input image, resizes it and detect edges with both cv::Canny and CUDA Canny Edge Detector object and log results to a .txt file. My image is 960x585 and 66.3 KB. I used C++ standard chrono library to measure the time spent at Edge Detection process and results show that the time spent at GPU is far more greater than the time spent at CPU. My code and results are given below. Are my results normal or am I doing something very wrong?

Laptop specs;

8 GB RAM

Intel i74700 MQ CPU 2.40 GHz

NVIDIA Geforce GT 745M GPU

#include <stdio.h>
#include <opencv2\core\core.hpp>
#include <opencv2\core\cuda.hpp>
#include <opencv2\imgproc.hpp>
#include <opencv2\opencv.hpp>
#include <chrono>
#include <fstream>


#define SIZE 25

int main()
{
    cv::Mat ImageHost = cv::imread("C:\\Users\\Heisenberg\\Desktop\\revan.jpg", CV_LOAD_IMAGE_GRAYSCALE);



        cv::Mat ImageHostArr[SIZE];

        cv::cuda::GpuMat ImageDev;
        cv::cuda::GpuMat ImageDevArr[SIZE];

        ImageDev.upload(ImageHost);


        for (int n = 1; n < SIZE; n++)
            cv::resize(ImageHost, ImageHostArr[n], cv::Size(), 0.5*n, 0.5*n, CV_INTER_LINEAR);


        for (int n = 1; n < SIZE; n++)
            cv::cuda::resize(ImageDev, ImageDevArr[n], cv::Size(), 0.5*n, 0.5*n, CV_INTER_LINEAR); 

        cv::Mat Detected_EdgesHost[SIZE];
        cv::cuda::GpuMat Detected_EdgesDev[SIZE];

        std::ofstream File1, File2;

        File1.open("C:\\Users\\Heisenberg\\Desktop\\canny_cpu.txt");
        File2.open("C:\\Users\\Heisenberg\\Desktop\\canny_gpu.txt");


        std::cout << "Process started... \n" << std::endl;
        for (int n = 1; n < SIZE; n++) {
            auto start = std::chrono::high_resolution_clock::now();
            cv::Canny(ImageHostArr[n], Detected_EdgesHost[n], 2.0, 100.0, 3, false);
            auto finish = std::chrono::high_resolution_clock::now();
            std::chrono::duration<double> elapsed_time = finish - start;
            File1 << "Image Size: " << ImageHostArr[n].rows* ImageHostArr[n].cols << "  " << "Elapsed Time: " << elapsed_time.count() * 1000 << " msecs" << "\n" << std::endl;
        }


        cv::Ptr<cv::cuda::CannyEdgeDetector> canny_edg = cv::cuda::createCannyEdgeDetector(2.0, 100.0, 3, false);   



        for (int n = 1; n < SIZE; n++) {
            auto start = std::chrono::high_resolution_clock::now();
            canny_edg->detect(ImageDevArr[n], Detected_EdgesDev[n]);
            auto finish = std::chrono::high_resolution_clock::now();
            std::chrono::duration<double> elapsed_time = finish - start;
            File2 << "Image Size: " << ImageDevArr[n].rows* ImageDevArr[n].cols << "  " << "Elapsed Time: " << elapsed_time.count() * 1000 << " msecs" << "\n" << std::endl;
        }
        std::cout << "Process ended... \n" << std::endl;



    return 0;
}
2019-03-08 02:26:31 -0500 edited question Assertion Failed pyrUp(); pyrDown();

Assertion Failed pyrUp(); pyrDown(); Hello there. I was trying to upsample and downsample an image with size of 256 x 25

2019-03-08 02:16:40 -0500 marked best answer Assertion Failed pyrUp(); pyrDown();

Hello there. I was trying to upsample and downsample an image with size of 256 x 256. My code is below. While it works perfect for 256 x 256 to 512 x 512, beside this code gives error below.

#include "iostream"  
#include "opencv2/imgproc.hpp"  
#include "opencv2/imgcodecs.hpp"  
#include "opencv2/highgui.hpp"  

int main()  
{  
 cv::Mat image = cv::imread("C:\\Users\\XXX\\Desktop\\OpenCV_Workspace\\cv_Project_I\\block.png");  

cv::Mat imageup2 = image.clone();  
cv::Mat imageup4 = image.clone();  
cv::Mat imageup5_6 = image.clone();  
cv::Mat imagedown0_5 = image.clone();  
cv::Mat imagedown0_25 = image.clone();  
cv::Mat imagedown0_12 = image.clone();  

//Upsample by 2, 4, 5.6  
cv::pyrUp(image, imageup2, cv::Size(image.cols * 2, image.rows * 2 ));  
cv::pyrUp(image, imageup4, cv::Size(image.cols * 4, image.rows * 4));  
cv::pyrUp(image, imageup5_6, cv::Size(image.cols * 5.6, image.rows * 5.6));  

//Downsample by 0.5, 0.25, 0.12  
cv::pyrDown(image, imagedown0_5, cv::Size(image.cols * 0.5, image.rows * 0.5));  
cv::pyrDown(image, imagedown0_25, cv::Size(image.cols * 0.25, image.rows * 0.25));  
cv::pyrDown(image, imagedown0_12, cv::Size(image.cols * 0.12, image.rows * 0.12));  

//Displaying Results  
cv::imshow("Original Image", image);  
cv::imshow("Image Upsampled by 2", imageup4);  
cv::imshow("Image Upsampled by 4", imageup4);  
cv::imshow("Image Upsampled by 5.6", imageup5_6);  
cv::imshow("Image Downsampled by 0.5", imagedown0_5);  
cv::imshow("Image Downsampled by 0.25", imagedown0_25);  
cv::imshow("Image Downsampled by 0.12", imagedown0_12);  
cv::waitKey();  

return 0;  
}

And this is the error code:

OpenCV(4.0.1) Error: Assertion failed (std::abs(dsize.width - ssize.width*2) == dsize.width % 2 && std::abs(dsize.height - ssize.height*2) == dsize.height % 2) in cv::pyrUp_, file c:\build\master_winpack-build-win64-vc15\opencv\modules\imgproc\src\pyramids.cpp, line 537   OpenCV: terminate handler is called! The last OpenCV error is: 
OpenCV(4.0.1) Error: Assertion failed (std::abs(dsize.width - ssize.width*2) == dsize.width % 2 && std::abs(dsize.height - ssize.height*2) == dsize.height % 2) in cv::pyrUp_, file c:\build\master_winpack-build-win64-vc15\opencv\modules\imgproc\src\pyramids.cpp, line 537

What am I doing wrong? Thanks for help.

2019-03-08 02:16:40 -0500 received badge  Scholar (source)
2019-03-08 02:16:38 -0500 commented answer Assertion Failed pyrUp(); pyrDown();

Before I read your answer I already used cv::resize(); and got the results I expected. Now I also tried it with cv::pyrU

2019-03-08 00:34:52 -0500 received badge  Supporter (source)
2019-03-07 17:06:08 -0500 received badge  Student (source)
2019-03-07 15:28:28 -0500 edited question Assertion Failed pyrUp(); pyrDown();

Assertion Failed pyrUp(); Hello there. I was trying to upsample and downsample an image with size of 256 x 256. My code

2019-03-07 15:26:26 -0500 asked a question Assertion Failed pyrUp(); pyrDown();

Assertion Failed pyrUp(); Hello there. I was trying to upsample and downsample an image with size of 256 x 256. My code

2019-03-07 11:55:32 -0500 received badge  Enthusiast
2019-01-22 05:22:42 -0500 received badge  Popular Question (source)
2017-04-24 13:10:23 -0500 commented answer CUDA Canny Edge Detector is slower than cv::Canny

4- I was also thinking about the same .

2017-04-24 13:04:06 -0500 commented answer CUDA Canny Edge Detector is slower than cv::Canny
 canny_edg->detect(ImageDevArr[1], Detected_EdgesDev[1]);

for (int n = 1; n < SIZE; n++) {
        auto start = std::chrono::high_resolution_clock::now();
        canny_edg->detect(ImageDevArr[n], Detected_EdgesDev[n]);
        auto finish = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> elapsed_time = finish - start;
        File2 << "Image Size: " << ImageDevArr[n].rows* ImageDevArr[n].cols << "  " << "Elapsed Time: " << elapsed_time.count() * 1000 << " msecs" << "\n" << std::endl;
}  <code>