void Binarization::Close(unsigned char *buffer_in, unsigned char *buffer_out, bool inverse) { Dilate(buffer_in, buffer_out, inverse); for(int i = 0; i < iterations_closing_ - 1; i++) Dilate(buffer_out, buffer_out, inverse); for(int i = 0; i < iterations_closing_; i++) Erode(buffer_out, buffer_out, inverse); }
Image::Pointer VolumeVisualizationImagePreprocessor::Process( Image::Pointer m_originalCT, Image::Pointer m_originalLiverMask) { VVP_INFO << "Processing..."; // converting mitk image -> itk image CTImage::Pointer CTImageWork = CTImage::New(); CastToItkImage( m_originalCT, CTImageWork ); // converting mitk image -> itk image BinImage::Pointer BinImageMask = BinImage::New(); CastToItkImage( m_originalLiverMask, BinImageMask ); DetermineBoundingBox( BinImageMask ); if( m_MaxX < m_MinX || m_MaxY < m_MinY || m_MaxZ < m_MinZ ) return 0; CTImageWork = Gaussian(Crop( CTImageWork )); BinImageMask = Crop( BinImageMask ); CTImage::Pointer itkResult =Composite(CTImageWork,BinImageMask,Dilate(BinImageMask),Erode(BinImageMask)); mitk::Image::Pointer mitkResult= mitk::Image::New(); mitk::CastToMitkImage( itkResult, mitkResult ); //TODO here we can perhaps save memory VVP_INFO << "Finished..."; return mitkResult; }
void Binarization::FillHole(unsigned char *buffer_in, unsigned char *buffer_out, bool inverse) { Replace(buffer_in, GetBuffer(BUFFER_INTERMEDIATE)); unsigned char *buffer = new unsigned char[width_ * height_ * 3]; memcpy(buffer, buffer_in, width_ * height_ * 3); for(int i = 0; i < iterations_hole_filling_; i++) { Dilate(buffer_in, buffer_out, inverse); for(int i = 1; i < height_ - 1; i++) for(int j = 1; j < width_ - 1; j++) { buffer_in[(i * width_ + j) * 3 + 2] = inverse ? buffer_out[(i * width_ + j) * 3 + 2] | ~buffer[(i * width_ + j) * 3 + 2] : buffer_out[(i * width_ + j) * 3 + 2] & ~buffer[(i * width_ + j) * 3 + 2]; buffer_in[(i * width_ + j) * 3 + 1] = inverse ? buffer_out[(i * width_ + j) * 3 + 1] | ~buffer[(i * width_ + j) * 3 + 1] : buffer_out[(i * width_ + j) * 3 + 1] & ~buffer[(i * width_ + j) * 3 + 1]; buffer_in[(i * width_ + j) * 3 + 0] = inverse ? buffer_out[(i * width_ + j) * 3 + 0] | ~buffer[(i * width_ + j) * 3 + 0] : buffer_out[(i * width_ + j) * 3 + 0] & ~buffer[(i * width_ + j) * 3 + 0]; } } for(int i = 1; i < height_ - 1; i++) for(int j = 1; j < width_ - 1; j++) { buffer_out[(i * width_ + j) * 3 + 2] = inverse ? buffer[(i * width_ + j) * 3 + 2] & buffer_in[(i * width_ + j) * 3 + 2] : buffer[(i * width_ + j) * 3 + 2] | buffer_in[(i * width_ + j) * 3 + 2]; buffer_out[(i * width_ + j) * 3 + 1] = inverse ? buffer[(i * width_ + j) * 3 + 1] & buffer_in[(i * width_ + j) * 3 + 1] : buffer[(i * width_ + j) * 3 + 1] | buffer_in[(i * width_ + j) * 3 + 1]; buffer_out[(i * width_ + j) * 3 + 0] = inverse ? buffer[(i * width_ + j) * 3 + 0] & buffer_in[(i * width_ + j) * 3 + 0] : buffer[(i * width_ + j) * 3 + 0] | buffer_in[(i * width_ + j) * 3 + 0]; } delete[] buffer; }
int DilateFile(const char *pFileName) { png_t Png; CPixel *pBuffer[3] = {0,0,0}; png_init(0, 0); png_open_file(&Png, pFileName); if(Png.color_type != PNG_TRUECOLOR_ALPHA) { dbg_msg("dilate", "%s: not an RGBA image", pFileName); return 1; } pBuffer[0] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1); pBuffer[1] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1); pBuffer[2] = (CPixel*)mem_alloc(Png.width*Png.height*sizeof(CPixel), 1); png_get_data(&Png, (unsigned char *)pBuffer[0]); png_close_file(&Png); int w = Png.width; int h = Png.height; Dilate(w, h, pBuffer[0], pBuffer[1]); for(int i = 0; i < 5; i++) { Dilate(w, h, pBuffer[1], pBuffer[2]); Dilate(w, h, pBuffer[2], pBuffer[1]); } CopyAlpha(w, h, pBuffer[0], pBuffer[1]); // save here png_open_file_write(&Png, pFileName); png_set_data(&Png, w, h, 8, PNG_TRUECOLOR_ALPHA, (unsigned char *)pBuffer[1]); png_close_file(&Png); return 0; }
/*! * \brief Closing * \param width * \param height * \param radius * \return */ void processimage::Closing( unsigned char* bmp, int width, int height, unsigned char* buffer, int radius, unsigned char* result) { unsigned char *img1 = new unsigned char[width * height]; Dilate(bmp, width, height, buffer, radius, img1); Erode(img1, width, height, buffer, radius, result); delete[] img1; }
void Binarization::Dilate(unsigned char *buffer_in, unsigned char *buffer_out, bool inverse) { TEST_OVERLAP(Dilate(buffer_in, buffer_out, inverse)); for(int i = 1; i < height_ - 1; i++) for(int j = 1; j < width_ - 1; j++) if(SatisfyDilation(buffer_in, i, j, inverse)) { buffer_out[(i * width_ + j) * 3 + 2] = inverse ? 0 : 255; buffer_out[(i * width_ + j) * 3 + 1] = inverse ? 0 : 255; buffer_out[(i * width_ + j) * 3 + 0] = inverse ? 0 : 255; } else { buffer_out[(i * width_ + j) * 3 + 2] = inverse ? 255 : 0; buffer_out[(i * width_ + j) * 3 + 1] = inverse ? 255 : 0; buffer_out[(i * width_ + j) * 3 + 0] = inverse ? 255 : 0; } }
//连通分量获取 void GetConCompG_Onent(double *src,double *dst,int width,int height,Position *seed){ double *temp=(double *)malloc(sizeof(double)*width*height); Zero(temp, width, height); double *lasttemp=(double*)malloc(sizeof(double)*width*height); temp[seed->y*width+seed->x]=255.0; double se[9]={255.,255.,255.,255.,255.,255.,255.,255.,255.}; while(!matrixisEqu(lasttemp, temp, width, height)){ matrixCopy(temp, lasttemp, width, height); Dilate(temp, width, height, temp, width, height, se, 3, 3, NULL); And(temp, src, temp, width, height); } matrixCopy(temp, dst, width, height); free(temp); free(lasttemp); }
//孔洞填充 void FillHole(double *src,double *dst,int width,int height,Position *seed){ double *temp=(double *)malloc(sizeof(double)*width*height); Zero(temp, width, height); double *lasttemp=(double *)malloc(sizeof(double)*width*height); double *nsrc=(double *)malloc(sizeof(double)*width*height); Not(src, nsrc, width, height); temp[seed->y*width+seed->x]=255.; double se[9]={255.,255.,255.,255.,255.,255.,255.,255.,255.}; while(!matrixisEqu(lasttemp, temp, width, height)){ matrixCopy(temp, lasttemp, width, height); Dilate(temp, width, height, temp, width, height, se, 3, 3, NULL); And(temp, nsrc, temp, width, height); } Or(temp, src, dst, width, height); free(temp); free(lasttemp); free(nsrc); }
//重建开操作 void reBuildOpen(double *src,double *dst,double *ground,int width,int height,double *dilateSE,int dse_width,int dse_height,double *erodeSE,int ese_width,int ese_height,int eroden){ double *temp=(double*)malloc(sizeof(double)*width*height); double *temp_last=(double*)malloc(sizeof(double)*width*height); matrixCopy(src, temp, width, height); for(int i=0;i<eroden;i++){ Erode(temp, width, height, temp,width, height, erodeSE, ese_height, ese_height, NULL); } while(!matrixisEqu(temp, temp_last,width,height)){ matrixCopy(temp, temp_last, width, height); Dilate(temp, width, height, temp, width, height, dilateSE, dse_width, dse_height, NULL); And(temp, ground, temp, width, height); } matrixCopy(temp, dst, width, height); free(temp); free(temp_last); }
void FaceMasker::Run(int min_depth, int min_pixels, int open_size, int head_width, int head_height, int head_depth, int face_size, int extended_size, int window_size, int width, int height, float focal_length, const Depth *depth, Color *color) { width_ = width; height_ = height; window_size_ = window_size; if (size_ < (width_ * height_)) { size_ = width_ * height_; if (valid_mask_ != NULL) delete [] valid_mask_; if (head_mask_ != NULL) delete [] head_mask_; if (depth_integral_ != NULL) delete [] depth_integral_; if (valid_integral_ != NULL) delete [] valid_integral_; if (head_integral_ != NULL) delete [] head_integral_; if (min_sizes_ != NULL) delete [] min_sizes_; if (max_sizes_ != NULL) delete [] max_sizes_; valid_mask_ = new bool[size_]; head_mask_ = new bool[size_]; depth_integral_ = new int[size_]; valid_integral_ = new int[size_]; head_integral_ = new int[size_]; min_sizes_ = new float[size_]; max_sizes_ = new float[size_]; } int max_depth = (head_width * focal_length) / min_pixels; #pragma omp parallel for for (int i = 0; i < (width * height); i++) { valid_mask_[i] = ((depth[i] > min_depth) && (depth[i] < max_depth)) ? true : false; } Integral(width, height, true, valid_mask_, valid_integral_); Integral(width, height, valid_mask_, depth, depth_integral_); #pragma omp parallel for for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int i = x + y * width; head_mask_[i] = false; if (valid_mask_[i]) { int head_cols = (int)((head_width * focal_length) / depth[i]); int head_rows = (int)((head_height * focal_length) / depth[i]); int center_average = Mean(width, height, x - head_cols / 2, x + head_cols / 2, y - head_rows / 2, y + head_rows / 2, depth_integral_, valid_integral_); int left_average = Mean(width, height, x - (5 * head_cols / 4), x - (3 * head_cols / 4), y - head_rows / 2, y + head_rows / 2, depth_integral_, valid_integral_); int right_average = Mean(width, height, x + (3 * head_cols / 4), x + (5 * head_cols / 4), y - head_rows / 2, y + head_rows / 2, depth_integral_, valid_integral_); int top_average = Mean(width, height, x - head_cols / 2, x + head_cols / 2, y - (5 * head_rows / 4), y - (3 * head_rows / 4), depth_integral_, valid_integral_); int top_left_average = Mean(width, height, x - (5 * head_cols / 4), x - (3 * head_cols / 4), y - (5 * head_rows / 4), y - (3 * head_rows / 4), depth_integral_, valid_integral_); int top_right_average = Mean(width, height, x + (3 * head_cols / 4), x + (5 * head_cols / 4), y - (5 * head_rows / 4), y - (3 * head_rows / 4), depth_integral_, valid_integral_); int center_difference = ABS(depth[i] - center_average); int left_difference = ABS(depth[i] - left_average); int right_difference = ABS(depth[i] - right_average); int top_difference = ABS(depth[i] - top_average); int top_left_difference = ABS(depth[i] - top_left_average); int top_right_difference = ABS(depth[i] - top_right_average); int alpha = head_depth; int beta = 2 * head_depth; head_mask_[i] = ((center_difference < alpha) && (left_difference > beta) && (right_difference > beta) && (top_difference > beta) && (top_left_difference > beta) && (top_right_difference > beta)) ? true : false; } } } Integral(width, height, false, head_mask_, head_integral_); Erode(width, height, open_size, head_integral_, head_mask_); Integral(width, height, true, head_mask_, head_integral_); Dilate(width, height, open_size, head_integral_, head_mask_); Integral(width, height, true, head_mask_, head_integral_); #pragma omp parallel for for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int i = x + y * width; min_sizes_[i] = max_sizes_[i] = 0.0f; if (valid_mask_[i]) { int face_pixels = (int)((face_size * focal_length) / depth[i]); if (Sum(width, height, x - face_pixels / 2, x + face_pixels / 2, y - face_pixels / 2, y + face_pixels / 2, head_integral_) > 0) { int extended_pixels =(int)((extended_size * focal_length) / depth[i]); min_sizes_[i] = face_pixels - extended_pixels; max_sizes_[i] = face_pixels + extended_pixels; } } } } frame_++; scale_ = 0; //#define HEAD_DEBUG #ifdef HEAD_DEBUG Mat image(height_, width_, CV_8UC3, color); Mat head_region = Mat::zeros(height_, width_, CV_8UC3); #pragma omp parallel for for (int y = 0; y < height_; y++) { for (int x = 0; x < width_; x++) { int i = x + y * width_; if (head_mask_[i]) head_region.at<Vec3b>(y, x) = Vec3b(255, 255, 255); } } char filename[256]; sprintf(filename, "head-%d.png", frame_); Mat output; addWeighted(image, 0.5, head_region, 0.5, 0.0, output); imwrite(filename, output); scale_++; #endif }
//关操作 void Close(double *src,int width,int height,double *dst,double *se,int se_width,int se_height,Position *center){ Dilate(src,width,height, dst,width,height,se,se_width,se_height, center); Erode(dst,width,height, dst,width,height,se,se_width,se_height, center); }
void Optimize0(){ //消除杂物 Dilate(2, 6 - (MAXWIDTH / w - 1), 4 - (MAXWIDTH / w - 1)); Erode(2, 2 - (MAXWIDTH / w - 1), 10 - (MAXWIDTH / w - 1)); }