Ask Your Question
0

a wierd result of a project with GpuMat and cuda

asked 2014-03-17 14:05:25 -0600

我干过豪哥 gravatar image

updated 2014-03-17 14:28:39 -0600

The code like following

#include <opencv2/core/core.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <opencv2/core/cuda_devptrs.hpp>
#include <opencv2/gpu/stream_accessor.hpp>
#include <opencv2/gpu/device/common.hpp>  
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <cuda_runtime.h>
using namespace std;
using namespace cv;
using namespace cv::gpu;

__global__ void AddColor_Kernel(const PtrStepSz<uchar3> src,const PtrStepSz<uchar> mask, 
PtrStep<uchar3> dst, PtrStep<uchar> dst_mask)
{
    int x = blockIdx.x * blockDim.x + threadIdx.x;
    int y = blockIdx.y * blockDim.y + threadIdx.x;

    if(y < mask.rows && x < mask.cols)
    {
        if(mask(y,x))
            dst.ptr(y )[ x] = src.ptr(y)[x];
        dst_mask.ptr(y )[ x] |= mask.ptr(y)[x];
    }
}




void AddColorCaller(const PtrStepSz<uchar3> &src,const PtrStepSz<uchar> &mask, PtrStep<uchar3> dst,
PtrStep<uchar> dst_mask,cudaStream_t stream)
    {

    dim3 block(32,8);
    //  dim3 grid(divUp(src.cols ,block.x),divUp(src.rows ,block.y));
    dim3 grid((mask.cols + block.x - 1)/block.x,(mask.rows + block.y - 1)/block.y);
    AddColor_Kernel<<<grid,block,0,stream>>>(src,mask,dst,dst_mask);
    cudaSafeCall(cudaGetLastError());
    if(stream == 0)
        cudaSafeCall(cudaDeviceSynchronize());
}

void AddColor(const GpuMat &src,const GpuMat &mask,  GpuMat &dst,  GpuMat &dst_mask,
Stream& stream = Stream::Null())
    {   
        dst.create(src.size(),src.type());
        dst.setTo(Scalar::all(0));
        dst_mask.create(mask.size(),mask.type());
        dst_mask.setTo(Scalar::all(0));
        cudaStream_t st = StreamAccessor::getStream(stream);
        AddColorCaller(src,mask,dst,dst_mask,st);
    }

int main()
{
    Mat image = imread("1.jpg");
    Mat mask  = imread("mask.jpg",-1);
    cout<<mask.channels()<<endl;
    resize(mask,mask,image.size());

    imshow("src",image);
    imshow("mask",mask);
    GpuMat gpuMat,output,mask_gpu,dst,dst_mask;

    gpuMat.upload(image);
    mask_gpu.upload(mask);
    AddColor(gpuMat,mask_gpu,dst,dst_mask);
    namedWindow("1",CV_WINDOW_OPENGL);
    imshow("1",dst);
    waitKey(0); 
    return 0;
}

origin_mask processed_final_mask image description

According to code,the final_mask should be the same as origin_mask.The pic should be cut according to the mask.But result is disappointed,So I hope somebody can point out error of my code.Thanks!

edit retag flag offensive close merge delete

Comments

so far , so bad. so what's your problem now ?

berak gravatar imageberak ( 2014-03-17 14:10:39 -0600 )edit

I edit my post and pic ^_^

我干过豪哥 gravatar image我干过豪哥 ( 2014-03-17 14:23:59 -0600 )edit

ah, better ;)

berak gravatar imageberak ( 2014-03-17 14:25:11 -0600 )edit

I am new to the GpuMat struct and PtrStep,hope you can figure out my fault.

我干过豪哥 gravatar image我干过豪哥 ( 2014-03-17 14:34:13 -0600 )edit

Hi, I am also new to GpuMat struct and PtrStep so I am wondering if your above code sucessfully compiled or not with NVCC? If you were able to execute it were you able to link all the things in a cmake file? If yes, It would be of a great help if you could share it with me privately via email. But still please tell if you were able to solve the Mat and PtrStep issue? Or is there an alternative to it? Thanks!

sarthakahuja11 gravatar imagesarthakahuja11 ( 2016-10-01 14:52:36 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2014-03-18 12:47:26 -0600

Vladislav Vinogradov gravatar image

Use IMREAD_GRAYSCALE and IMREAD_COLOR flags for imread calls:

Mat image = imread("1.jpg", IMREAD_COLOR);
Mat mask  = imread("mask.jpg", IMREAD_GRAYSCALE);
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-03-17 14:05:25 -0600

Seen: 1,384 times

Last updated: Mar 18 '14