void Binarization::Open(unsigned char *buffer_in, unsigned char *buffer_out, bool inverse) { Erode(buffer_in, buffer_out, inverse); for(int i = 0; i < iterations_opening_ - 1; i++) Erode(buffer_out, buffer_out, inverse); for(int i = 0; i < iterations_opening_; i++) Dilate(buffer_out, buffer_out, inverse); }
//击中与击不中 void HitorMiss(double *src,int width,int height,double *se1,int se1_width,int se1_height,double *se2,int se2_width,int se2_height,double *dst,Position *se1center,Position *se2center){ double *temp1=(double *)malloc(sizeof(double)*width*height); double *temp2=(double *)malloc(sizeof(double)*width*height); Erode(src, width, height, temp1, width, height, se1, se1_width, se1_height, se1center); Not(src, temp2, width, height); Erode(temp2, width, height, temp2, width, height, se2, se2_width, se2_height, se2center); And(temp1, temp2, dst, width, height); free(temp1); free(temp2); }
void Binarization::HMT(unsigned char *buffer_in, unsigned char *buffer_out, bool inverse) { set_structure_element(mixture_structure_element_2_); unsigned char *buffer = GetBuffer(BUFFER_INTERMEDIATE - 1); Invert(buffer_in, buffer); Erode(buffer, buffer, inverse); set_structure_element(mixture_structure_element_1_); Erode(buffer_in, buffer_out, inverse); 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_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_out[(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_out[(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]; } }
//二值图像,边缘检测 void BinaryEdge(double *src,int width,int height,double* dst){ double *temp=(double *)malloc(sizeof(double)*width*height); double se[9]={255.,255.,255.,255.,255.,255.,255.,255.,255.}; Erode(src, width, height, temp, width, height,se ,3, 3, NULL); matrixSub(src, temp, dst, width, height); free(temp); }
void Binarization::ExtractBoundary(unsigned char *buffer_in, unsigned char *buffer_out, bool inverse) { TEST_OVERLAP(ExtractBoundary(buffer_in, buffer_out, inverse)); Erode(buffer_in, buffer_out, inverse); for(int i = 1; i < height_ - 1; i++) for(int j = 1; j < width_ - 1; j++) { buffer_out[(i * width_ + j) * 3 + 2] = (buffer_out[(i * width_ + j) * 3 + 2] ^ buffer_in[(i * width_ + j) * 3 + 2]) ? 255 : 0; buffer_out[(i * width_ + j) * 3 + 1] = (buffer_out[(i * width_ + j) * 3 + 1] ^ buffer_in[(i * width_ + j) * 3 + 1]) ? 255 : 0; buffer_out[(i * width_ + j) * 3 + 0] = (buffer_out[(i * width_ + j) * 3 + 0] ^ buffer_in[(i * width_ + j) * 3 + 0]) ? 255 : 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::Erode(unsigned char *buffer_in, unsigned char *buffer_out, bool inverse) { TEST_OVERLAP(Erode(buffer_in, buffer_out, inverse)); for(int i = 1; i < height_ - 1; i++) for(int j = 1; j < width_ - 1; j++) if(SatisfyErosion(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 FrameWork(double *src,double *dst,int width,int height,double *se,int se_width,int se_height){ double *temp_dst=(double*)malloc(sizeof(double)*width*height); Zero(temp_dst, width, height); double *temp=(double *)malloc(sizeof(double)*width*height); double *temp_open=(double *)malloc(sizeof(double)*width*height); matrixCopy(src, temp, width, height); while(!matrixisEmpty(temp,width,height)){ Erode(temp, width, height, temp, width, height, se, se_width, se_height, NULL); matrixCopy(temp, temp_open, width, height); Open(temp_open, width, height, temp_open, se, se_width, se_height, NULL); matrixSub(temp, temp_open,temp_open, width, height); Or(temp_open, temp_dst, temp_dst, width, height); } matrixCopy(temp_dst, dst, width, height); free(temp); free(temp_open); free(temp_dst); }
//重建开操作 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 Convexhull(double *src,double *dst,int width,int height){ double * temp_dst=(double *)malloc(sizeof(double)*width*height); matrixCopy(src, temp_dst, width, height); double * se[4]; 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<4;i++){ se[i]=CreateConvexhullSE(i); while (!matrixisEqu(temp, temp_last, width, height)) { matrixCopy(temp, temp_last, width, height); Erode(temp, width,height, temp, width, height, se[i], 3, 3, NULL); Or(temp, temp_dst, temp, width, height); } matrixCopy(temp, temp_dst, width, height); free(se[i]); } matrixCopy(temp_dst, dst, width, height); free(temp); free(temp_last); }
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 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)); }