Hi there,
I’m doing morphological operation on a image with some functions, but I get a weired result and don’t know why. Here’s my code:
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <nppi.h>
#include <npp.h>
#include <helper_string.h>
#include <helper_cuda.h>
#include <ImageIO.h>
#include <ImagesNPP.h>
#include <ImagesCPU.h>
#include <cuda_runtime.h>
int main()
{
std::string file_src(".\\test3_initial.bmp");
npp::ImageCPU_8u_C1 Host_Src;
//read image from disk to Cpu
npp::loadImage(file_src, Host_Src);
npp::ImageCPU_8u_C1 Host_Dst(Host_Src.size());
//Copy image from Cpu to Gpu
npp::ImageNPP_8u_C1 Device_Src_8u(Host_Src);
//-----------------------------Var_Threshold-----------------------------------//
npp::ImageNPP_8u_C1 Close_mask(25, 25); //close morph-operation mask
npp::ImageNPP_8u_C1 result1(Device_Src_8u.size()); //the result after 3x3 erode
npp::ImageNPP_8u_C1 result2(Device_Src_8u.size()); //the result after 3x3 dilate
npp::ImageNPP_8u_C1 result3(Device_Src_8u.size()); //the result after MorphCloseBorder
//npp::ImageCPU_8u_C1 mask_tes(Close_mask.size());
NppiSize Src_size = { (int)Device_Src_8u.width(), (int)Device_Src_8u.height() };
NppiSize roi_size = { (int)Device_Src_8u.width(), (int)Device_Src_8u.height() };
NppiPoint offset = { 0, 0 };
NppiPoint anchor = { 0, 0 };
NppiSize close_mask_size = { (int)Close_mask.width(), (int)Close_mask.height() };
NppiPoint close_mask_anchor = { (close_mask_size.width - 1) / 2, (close_mask_size.height - 1) / 2 };
int hpbuffersize;
Npp8u* pBuffer;
NppStatus err;
//-----------------------------Var_Threshold-----------------------------------//
//------------------------------Declare Over-------------------------//
//-----------------------------Var_Threshold-----------------------------------//
nppiSet_8u_C1R(1, Close_mask.data(), Close_mask.pitch(), close_mask_size);
nppiErode3x3Border_8u_C1R(
Device_Src_8u.data(),
Device_Src_8u.pitch(),
Src_size, offset,
result1.data(),
result1.pitch(),
roi_size, NPP_BORDER_REPLICATE
);
nppiDilate3x3Border_8u_C1R(
result1.data(),
result1.pitch(),
Src_size, offset,
result2.data(),
result2.pitch(),
roi_size, NPP_BORDER_REPLICATE
);
nppiMorphGetBufferSize_8u_C1R(roi_size, &hpbuffersize);
cudaMalloc((void**)(&pBuffer), hpbuffersize);
err = nppiMorphCloseBorder_8u_C1R(
result2.data(), result2.pitch(),
roi_size, offset,
result3.data(), result3.pitch(),
roi_size, Close_mask.data(), close_mask_size, close_mask_anchor,
pBuffer, NPP_BORDER_REPLICATE
);
result3.copyTo(Host_Dst.data(), Host_Dst.pitch());
//Save image
npp::saveImage(".\\test3_test.pgm", Host_Dst);
}
I first read the image(variable “Device_Src_8u”), do 3x3 erode and dilation(which means the open operation) with nppiErode3x3Border_8u_C1R() and nppiDilate3x3Border_8u_C1R(). Then I do close operation with a user-defined mask(25x25 rectangle). The result after close operation(variable “result3”) is extremely wired as there seems almost no difference after the operation in the vertical direction.
Also, I upload the Initial image, the result after open operation(result 2) and the result after the close operation(result 3):
Initial image: https://drive.google.com/open?id=1XEbel4P52nDUSztD18cNp-lUy9iNrJEU
Result2: https://drive.google.com/open?id=1CmC8KUScQ5vh5GMSZW4tEJx9ic-ddFsm
Result3: https://drive.google.com/open?id=1dB6OOxVO5YdiuKgbNfs_mwGSe5BHjNK3
Can anyone help me?
Thanks!