Ask Your Question

Digital Design's profile - activity

2020-09-22 07:16:28 -0500 received badge  Notable Question (source)
2019-12-18 16:40:23 -0500 received badge  Popular Question (source)
2019-06-29 07:08:41 -0500 commented answer GPU runs much slower than CPU

Sorry for my late reply. As cudawarped said, the CPU resolution isn't high enough to capture the elapsed time for GPU ev

2019-06-29 07:04:22 -0500 marked best answer GPU runs much slower than CPU

You may have seen this popular tutorial for GPU: https://devblogs.nvidia.com/even-easi... I tried to implement the addition kernel of this webpage and compare the processing time between CPU and GPU. Code:

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <conio.h>

#include <stdlib.h>
#include <conio.h>
#include < Windows.h>

#include <opencv2/core.hpp>
//#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

using namespace cv;

// Kernel function to add the elements of two arrays
__global__
void add(long int n, float *x, float *y)
{
    int index = threadIdx.x;
    int stride = blockDim.x;
    int i = blockIdx.x*blockDim.x + threadIdx.x;
    int STEP_LEN = blockDim.x* gridDim.x;
    //*xx = int( n / (STEP_LEN));
    for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += blockDim.x * gridDim.x)
    {
        y[i] = x[i] + y[i];
    }
    /*for (int j= 0; j< n/(STEP_LEN); j++)
    y[(j* STEP_LEN)+ i] = x[(j* STEP_LEN)+ i] + y[(j*STEP_LEN) + i];*/
}

int main(void)
{
    long int N = 1 << 20; // 1M elements

    double tt;
    float *x, *y;

    // Run kernel on 1M elements on the GPU
    int blockSize = 256;
    while (blockSize != 1)
    {
        cudaMallocManaged(&x, N * sizeof(float));
        cudaMallocManaged(&y, N * sizeof(float));

        // initialize x and y arrays on the host
        for (long int i = 0; i < N; i++) {
            x[i] = 1.0f;
            y[i] = 2.0f;
            //printf("fabs(y[%ld] - 3.0f)= %g\n", i, y[i] - 3.0f);
        }
        for (long int i = 0; i < N; i++)
            y[i] = 2.0f;
        std::cout << "Enter blockSize (1 to terminate): ";
        std::cin >> blockSize;
        int numBlocks = (N + blockSize - 1) / blockSize;
        tt = (double)getTickCount();
        add << <numBlocks, blockSize >> >(N, x, y);
        tt = ((double)getTickCount() - tt) / getTickFrequency();
        //add << <8, 64>> >(N, x, y);

        // Wait for GPU to finish before accessing on host
        cudaDeviceSynchronize();

        // Check for errors (all values should be 3.0f)
        float maxError = 0.0f;
        float net_err = 0;
        for (long int i = 0; i < N; i++)
        {
            //std::cout << "i1= " << (long int)(i) << ") " << y[i] << std::endl;
            maxError = fmax(maxError, fabs(y[i] - 3.0f));
            net_err += fabs(y[i] - 3.0f);
        }

        std::cout << "Max error: " << maxError << ", net_err= " << net_err << std::endl;
        std::cout << tt << "seconds spent ." << std::endl;
        std::cout << "------------------------------------------------------------------------------------" << std::endl;

        // Free memory
        cudaFree(x);
        cudaFree(y);
    }

    for (register int j1 = 0; j1 < 10; j1++)
    {
        x = (float*)malloc(N * sizeof(float));
        y = (float*)malloc(N * sizeof(float));
        for (register long int i = 0; i < N; i++)
            y[i] = 2.0f;

        tt = (double)getTickCount();
        for (register long int i = 0; i < N; i++)
            y[i] = x[i] + y[i];
        tt = ((double)getTickCount() - tt) / getTickFrequency();
        std::cout << tt << "seconds spent ." << std::endl;
        std::cout << "******************************************************" << std::endl;
        free(x);
        free(y);
    }

    std::cout << "Press any key to finish..." << std::endl;
    getch();
    return 0;
}

"blockSize" is the number of ... (more)

2019-06-18 11:08:15 -0500 asked a question GPU runs much slower than CPU

GPU runs much slower than CPU You may have seen this popular tutorial for GPU: https://devblogs.nvidia.com/even-easier-i

2018-12-26 01:18:29 -0500 asked a question specify the compression ratio of JPEG 2000

specify the compression ratio of JPEG 2000 Hi all, I am trying to compress an RGB image with JPEG 2000 in opencv c++. M

2018-11-19 08:45:05 -0500 marked best answer Singular Value Decomposition (SVD) with opencv

Hi, I am using SVD for my algorithm and the decomposition is done by this instruction:

const int N= 16;
Mat x= (Mat_<double>(N, N)<< 23, 24, 24, 23, 22.5, 21.5, 20, 20, 19, 20, 22, 23,    23, 23.5, 24, 25.5,
        23.5, 24, 23, 21, 21, 22, 21, 20, 18.5, 18, 19, 20, 20.5, 23, 24, 24,
        24, 24, 22, 21, 21.5, 21.5, 21, 20, 19.5, 20, 21, 21, 22, 23, 24, 24,
        24.5, 24, 23, 23, 23, 21, 20, 20, 20, 22, 22, 22, 23, 23, 24, 24,
        24, 24, 24, 24, 24, 22, 20, 20, 20, 21, 21, 21, 21, 22, 24, 24,
        24, 24.5, 25, 24.5, 24, 22, 20, 21, 21, 21, 22, 22, 22, 23, 24, 24,
        24, 24.5, 25, 24.5, 24, 22, 20, 22, 23, 23, 24, 24, 23, 22, 22.5, 23,
        24.5, 24.5, 24, 24, 23, 22, 22, 23, 24.5, 24.5, 23, 21.5, 21, 21, 22.5, 22.5,
        23.5, 24.5, 25.5, 25, 23.5, 24.5, 26, 25.5, 24, 25, 22.5, 20.5, 20.5, 21, 21, 20.5,
        24.5, 25.5, 26, 25.5, 24.5, 25, 25.5, 25, 24, 24, 22, 20.5, 20.5, 19.5, 18.5, 19.5,
        25.5, 25.5, 26, 26, 25, 25, 25, 25, 23.5, 24, 22, 20, 20, 19.5, 18.5, 21,
        24, 24, 24, 24, 24, 24.5,   23, 22.5, 24, 24.5, 23, 20.5, 20, 20, 20.5, 22.5,
        24, 24, 24, 24, 24, 24, 23, 22.5, 24, 23.5, 23, 21.5, 20, 19.5, 22, 24,
        22.5, 23, 22.5, 22, 23, 23, 22, 21.5, 23, 22, 22, 22, 20, 20.5, 23, 24,
        22.5, 21.5, 21.5, 22, 23, 24, 22.5, 20, 21, 20.5, 20.5, 22, 22, 23, 23.5, 23.5,
        22.5, 21, 21, 21, 21, 23, 21.5, 20, 20, 20.5, 20.5, 22, 24, 24, 23.5, 23.5);
Mat Sigma= Mat::zeros(N, N, CV_64FC1);
Mat S, U, VT, x_hat, err;  
int minLoc[2], maxLoc[2];
double minVal, maxVal;
SVDecomp(x, S, U, VT, cv::SVD::FULL_UV);
/*Please note that S is a column vector and S should be a square diagonal matrix, Assigning the components of the S    to the diagonal components of Sigma*/
x_hat= U * Sigma* VT;
cout<< "U * Sigma* VT= "<< endl<< x_hat<< endl;
absdiff(x, x_hat, err);

And the error is really huge!!! (Terminal output)

Reconstruction err: MIN= 0.00135955, MAX= 2.99473, sum= [176.076, 0, 0, 0]

Which is shit! The whole process is done very straight forward with matlab. What's wrong with opencv SVD? Why does the operation loon irreversible?!!!! Your advices are highly appreciated.

minMaxIdx(err, &minVal, &maxVal, minLoc, maxLoc);
cout<< "Reconstruction err: MIN= "<< minVal<< ", MAX= "<< maxVal<< ", sum= "<< ...
(more)
2018-09-10 08:28:14 -0500 commented answer Singular Value Decomposition (SVD) with opencv

Many thanks for your answer. It works!! Just out of curiosity, why do you multiply each line of VT by corresponding sing

2018-09-10 05:05:28 -0500 asked a question Singular Value Decomposition (SVD) with opencv

Singular Value Decomposition (SVD) with opencv Hi, I am using SVD for my algorithm and the decomposition is done by this

2018-05-28 07:57:44 -0500 asked a question Problem with Adding DLL for 2D wavelet in opencv c++

Problem with Adding DLL for 2D wavelet in opencv c++ Hello, I am trying to use 2d wavelet transform for image processing

2018-01-10 10:41:36 -0500 asked a question Passing vector to the function

Passing vector to the function Hi there, Suppose a vector is consisted of 256 images. First, the blue channel of all ima

2017-12-06 14:15:05 -0500 commented answer Decomposing an image to several subimages

Thank you for your response. It was great :)

2017-12-05 12:10:14 -0500 asked a question Decomposing an image to several subimages

Decompsoing an image to several subimages Hello, I am gonna divide a 48004800 RGB image (Host_Image) to 1616 RGB sub-ima

2017-11-23 12:58:11 -0500 marked best answer Saving the image

Hi there, A very easy question. I´m trying to load an image and save it to a new variable/file.

int main()
{
    Mat host, dest;
    register unsigned long int i1, j1;
    vector<int> JPEG_param;
    host = imread("E://PO//2//Amir_3_0_0__0_16x16-des22_L1xL2_300x300_NxM_1000x1000_centrox10_centroy46.jpg", CV_LOAD_IMAGE_ANYCOLOR);
    dest = Mat::zeros(host.rows, host.cols, CV_8UC3);
    if (!(host.size() == dest.size()))
        cout << "Unequal Size" << endl;
    for (i1 = 0; i1 < host.rows* host.cols; i1++)
        host.data[i1] = dest.data[i1];

    namedWindow("The Host Image", WINDOW_AUTOSIZE);
    imshow("The Host Image", host);
    waitKey(0);
    destroyWindow("The Host Image");
    namedWindow("The dest Image", WINDOW_AUTOSIZE);
    imshow("The dest Image", dest);
    waitKey(0);
    destroyWindow("The dest Image");
    JPEG_param.push_back(CV_IMWRITE_JPEG_QUALITY);
    JPEG_param.push_back(100);
    imwrite("E://PO//123.jpg", dest, JPEG_param);
    getchar();
    return 0;
}

But when I display the image, it´s entirely black. What´s wrong???!!!

2017-11-23 08:39:58 -0500 commented answer Saving the image

Many thanks for your comprehensive explanations. Thank youuuuuuuuuuuuuuuuuuuuu :)

2017-11-23 08:38:52 -0500 received badge  Enthusiast
2017-11-22 07:42:28 -0500 commented question Saving the image

int main() { Mat host, dest; register unsigned long int i1, j1; vector<int> JPEG_param; host = imr

2017-11-22 07:33:15 -0500 asked a question Saving the image

Saving the image Hi there, A very easy question. I´m trying to load an image and save it to a new variable/file. int m

2017-10-16 10:03:50 -0500 commented question An array of Images

I used this page, but it sounds like the dimensions of the array should be known in advanced and it can't be changed on

2017-10-16 09:45:02 -0500 asked a question An array of Images

An array of Images Hi there, I know how to read and process a single image using Mat variables. Recently, I´ve a nightma

2017-10-16 09:20:59 -0500 marked best answer Pointer to Mat Variables

Hello, I want to add a dynamic array of Mat variebles, i.e. a 2D array of images. Simply suppose you are putting e.g. 3 rows and 4 columns of your images on the table. I write the following code:

Mat **img_array;
Mat temp = imread("Lena.tif", CV_LOAD_IMAGE_ANYDEPTH);
img_array = (Mat **)malloc(hor * sizeof(temp));
for (char i1 = 0;i1<hor;i1++)
{
    img_array[i1] = (Mat *)malloc(ver * sizeof(temp));
}
puts("Initializing Image Array...");
for (i1 = 0; i1 < hor; i1++)
    for (char j1 = 0; j1 < ver; j1++)
        img_array[i1][j1] = Mat::zeros(rows, col, CV_8UC3);

hor, ver are the nuber of horizontal and vertical elements of each array and rows, col refer to the number of the pixels of each individual image. When I run the program, the following error is issued: Exception thrown at 0x00007FFCFFA28BCC (opencv_world320d.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF. occurred Any Idea?

2017-10-13 04:55:02 -0500 asked a question Pointer to Mat Variables

Pointer to Mat Variables Hello, I want to add a dynamic array of Mat variebles, i.e. a 2D array of images. Simply suppos

2017-10-12 06:55:42 -0500 marked best answer 2D Array of Mat variables

Hi there, I am going to define my new class and my new class will have a public variable which is a 2D array of Mat. The main problem is that the dimensions of this array is not known in advanced, e.g. a class may need a 35 array and another class may need a 1012 array of Mat elements. How can I define such class? Any idea? I know that it should be fulfilled by pointers, but I failed to implement it so far |)

2017-10-11 11:23:57 -0500 commented answer 2D Array of Mat variables

Dear Break, Many thanks for your answer. But I don´t know the array dimensions and it may vary widely, e.g. [4][6] to [1

2017-10-11 11:00:40 -0500 commented question 2D Array of Mat variables

I use C++ with MS visual studio

2017-10-11 10:49:18 -0500 asked a question 2D Array of Mat variables

2D Array of Mat variables Hi there, I am going to define my new class and my new class will have a public variable which

2017-10-10 05:55:44 -0500 commented answer Accessing the elements of a Mat variable

Thanks a lot. I thought that the pixels are integer! You are absolutely right :)

2017-10-10 05:54:53 -0500 marked best answer Accessing the elements of a Mat variable

Hi, I´m gonna find the maximum and the minimum value of an image pixels: // ConsoleApplication1.cpp : Defines the entry point for the console application. //

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>


int main()
{
    Mat img1;
    double  minVal, maxVal;
    int minLoc[2], maxLoc[2];
    img1 = imread("DCIM_20171025_1.JPG");
    namedWindow("Test Image", WINDOW_AUTOSIZE);
    imshow("Test Image", img1);
    waitKey(0);
    minMaxIdx(img1, &minVal, &maxVal, minLoc, maxLoc);
    printf("MAX= %d, MIN= %d", maxVal, minVal);
    getch();
    return 0;
}

It seems pretty easy, but it doesn´t work! The program is compiled successfully, but there´ll be a run time error: Unhandled exception at 0x00007FF9E3EB9E08 in ConsoleApplication1.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000C981CFF3C0. occurred Does anyone know how to solve this problem? Thank you

2017-10-10 05:54:53 -0500 received badge  Scholar (source)
2017-10-10 03:54:25 -0500 asked a question Accessing the elements of a Mat variable

Accessing the elements of a Mat variable Hi, I´m gonna find the maximum and the minimum value of an image pixels: // Con

2017-06-19 01:41:54 -0500 commented question How to save an image with JPEG output in opencv 3.2.0?

please REMOVE those #pragma s and learn, how to add libraries properly to your ide.

then, you must NEVER use both debug and release libs at the same time. be strict about using the correct one only, or you'll burn in 0xC0000005:hell foerever ! I'm a beginner in opencv. Can you tell me more about debug/release mode? Many thanks to LBerger and break for their kind support

2017-06-19 01:40:33 -0500 commented question How to save an image with JPEG output in opencv 3.2.0?

Have you got only one pragma comment ? Your program is good (VS 2015 windows 10 opencv 3.2.0-dev) dll called is opencv_world320.dll you should be in release mode i tried to use each one separately, but the problem still persists...

2017-06-18 17:58:19 -0500 commented question How to save an image with JPEG output in opencv 3.2.0?

I am really confused. I couldn't follow the flow of the given link. all the other functions are running properly (resize, crop, ...). Do you think that the main reason of failure to save JPEG file is #progma command?

2017-06-18 15:06:32 -0500 commented question How to save an image with JPEG output in opencv 3.2.0?

Thank you for your help. But if I don't use #pragma comment(lib everything goes wrong and I end up with millions of errors! How can I complie my program without #progma?

2017-06-18 09:58:18 -0500 asked a question How to save an image with JPEG output in opencv 3.2.0?

Hello, I am using opencv 3.2.0 and I simply want to read an image and simple save the same image to JPEG output with compression ratio of 75%. There is an example for PNG format: http://docs.opencv.org/trunk/d4/da8/g... . I use the following instructions:

#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

#pragma comment(lib, "opencv_world320.lib")
#pragma comment(lib, "opencv_world320d.lib")

using namespace std;
using namespace cv;

int main()
{
    /************************************************************
    Declaring Variables
    ************************************************************/
    int x, y;
    Mat my_img;
    vector<int> compression_params;
    /************************************************************
    load and dispaly an image
    ************************************************************/
    my_img = imread("E:/Standard_Images/goldhill.jpg", CV_LOAD_IMAGE_COLOR);
    cout << "The image dimensions:" << my_img.rows << "*" << my_img.cols << endl;
    cout << "The number of the channels:" << my_img.channels() << endl;
    cout << "The image depth: " << my_img.depth() << endl;
    namedWindow("Lena", WINDOW_AUTOSIZE);
    imshow("Lena", my_img);
    waitKey(0);
    destroyWindow("Lena");
    /************************************************************
    Compress the image with JPEG and write into a new file
    ************************************************************/
    compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
    compression_params.push_back(75);
    bool bSuccess = imwrite("E:/Standard_Images/My_Image.jpg", my_img, compression_params);
    if (!bSuccess)
    {
        cout << "Couldn't save the file" << endl;
    }
    else
    {
        namedWindow("The Saved file", CV_WINDOW_AUTOSIZE);
        Mat openImage = imread("example.jpg", CV_LOAD_IMAGE_UNCHANGED);
        imshow("The Saved file", openImage);
    }
    getchar();
    return 1;
}

But it fails to save the output image and the following error is issued: "Exception thrown at 0x00007FFF08DC86C2 (opencv_world320.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000020F4BADF000." Do you have any idea about how to save an image with JPEG format? Thanks a lot for your help