mitk::CylindricToCartesianFilter::~CylindricToCartesianFilter() { if(rt_pic!=NULL) mitkIpPicFree(rt_pic); if(phit_pic!=NULL) mitkIpPicFree(phit_pic); if(fr_pic!=NULL) mitkIpPicFree(fr_pic); if(fphi_pic!=NULL) mitkIpPicFree(fphi_pic); if(coneCutOff_pic!=NULL) mitkIpPicFree(coneCutOff_pic); if(zt != NULL) free(zt); if(fz != NULL) free (fz); }
/** 3.1 Create a skeletonization of the segmentation and try to find a nice cut 3.1.1 Call a ipSegmentation algorithm to create a nice cut 3.1.2 Set the result of this algorithm as the feedback contour */ bool mitk::RegionGrowingTool::OnMousePressedInside( StateMachineAction*, InteractionEvent* interactionEvent, mitkIpPicDescriptor* workingPicSlice, int initialWorkingOffset) { if ( SegTool2D::CanHandleEvent(interactionEvent) < 1.0 ) return false; mitk::InteractionPositionEvent* positionEvent = dynamic_cast<mitk::InteractionPositionEvent*>( interactionEvent ); //const PositionEvent* positionEvent = dynamic_cast<const PositionEvent*>(stateEvent->GetEvent()); // checked in OnMousePressed // 3.1.1. Create a skeletonization of the segmentation and try to find a nice cut // apply the skeletonization-and-cut algorithm // generate contour to remove // set m_ReferenceSlice = NULL so nothing will happen during mouse move // remember to fill the contour with 0 in mouserelease mitkIpPicDescriptor* segmentationHistory = ipMITKSegmentationCreateGrowerHistory( workingPicSlice, m_LastWorkingSeed, NULL ); // free again if (segmentationHistory) { tCutResult cutContour = ipMITKSegmentationGetCutPoints( workingPicSlice, segmentationHistory, initialWorkingOffset ); // tCutResult is a ipSegmentation type mitkIpPicFree( segmentationHistory ); if (cutContour.cutIt) { int timestep = positionEvent->GetSender()->GetTimeStep(); // 3.1.2 copy point from float* to mitk::Contour ContourModel::Pointer contourInImageIndexCoordinates = ContourModel::New(); contourInImageIndexCoordinates->Expand(timestep + 1); contourInImageIndexCoordinates->SetClosed(true, timestep); Point3D newPoint; for (int index = 0; index < cutContour.deleteSize; ++index) { newPoint[0] = cutContour.deleteCurve[ 2 * index + 0 ] - 0.5;//correction is needed because the output of the algorithm is center based newPoint[1] = cutContour.deleteCurve[ 2 * index + 1 ] - 0.5;//and we want our contour displayed corner based. newPoint[2] = 0.0; contourInImageIndexCoordinates->AddVertex( newPoint, timestep ); } free(cutContour.traceline); free(cutContour.deleteCurve); // perhaps visualize this for fun? free(cutContour.onGradient); ContourModel::Pointer contourInWorldCoordinates = FeedbackContourTool::BackProjectContourFrom2DSlice( m_WorkingSlice->GetGeometry(), contourInImageIndexCoordinates, true ); // true: sub 0.5 for ipSegmentation correction FeedbackContourTool::SetFeedbackContour( *contourInWorldCoordinates ); FeedbackContourTool::SetFeedbackContourVisible(true); mitk::RenderingManager::GetInstance()->RequestUpdate( positionEvent->GetSender()->GetRenderWindow() ); m_FillFeedbackContour = true; } else { m_FillFeedbackContour = false; } } else { m_FillFeedbackContour = false; } m_ReferenceSlice = NULL; return true; }
void mitk::PicFileReader::ConvertHandedness(mitkIpPicDescriptor* pic) { //left to right handed conversion if(pic->dim>=3) { mitkIpPicDescriptor* slice=mitkIpPicCopyHeader(pic, NULL); slice->dim=2; size_t size=_mitkIpPicSize(slice); slice->data=malloc(size); size_t v, volumes = (pic->dim>3? pic->n[3] : 1); size_t volume_size = size*pic->n[2]; for(v=0; v<volumes; ++v) { unsigned char *p_first=(unsigned char *)pic->data; unsigned char *p_last=(unsigned char *)pic->data; p_first+=v*volume_size; p_last+=size*(pic->n[2]-1)+v*volume_size; size_t i, smid=pic->n[2]/2; for(i=0; i<smid;++i,p_last-=size, p_first+=size) { memcpy(slice->data, p_last, size); memcpy(p_last, p_first, size); memcpy(p_first, slice->data, size); } } mitkIpPicFree(slice); } }
void mitk::PicFileReader::FillImage(Image::Pointer output) { mitkIpPicDescriptor *outputPic = mitkIpPicNew(); outputPic = CastToIpPicDescriptor(output, nullptr, outputPic); mitkIpPicDescriptor *pic = mitkIpPicGet(const_cast<char *>(this->GetLocalFileName().c_str()), outputPic); // comes upside-down (in MITK coordinates) from PIC file ConvertHandedness(pic); mitkIpPicTSV_t *tsv; if ((tsv = mitkIpPicQueryTag(pic, "SOURCE HEADER")) != NULL) { if (tsv->n[0] > 1e+06) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag(pic, "SOURCE HEADER"); mitkIpPicFreeTag(tsvSH); } } if ((tsv = mitkIpPicQueryTag(pic, "ICON80x80")) != NULL) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag(pic, "ICON80x80"); mitkIpPicFreeTag(tsvSH); } if ((tsv = mitkIpPicQueryTag(pic, "VELOCITY")) != NULL) { mitkIpPicDescriptor *header = mitkIpPicCopyHeader(pic, NULL); header->data = tsv->value; ConvertHandedness(header); output->SetChannel(header->data, 1); header->data = NULL; mitkIpPicFree(header); mitkIpPicDelTag(pic, "VELOCITY"); } // Copy the memory to avoid mismatches of malloc() and delete[]. // mitkIpPicGet will always allocate a new memory block with malloc(), // but MITK Images delete the data via delete[]. output->SetImportChannel(pic->data, 0, Image::CopyMemory); pic->data = nullptr; mitkIpPicFree(pic); }
mitk::ImageDataItem::~ImageDataItem() { if(m_VtkImageData!=NULL) m_VtkImageData->Delete(); if(m_PicDescriptor!=NULL) { m_PicDescriptor->data=NULL; mitkIpPicFree(m_PicDescriptor); } if(m_Parent.IsNull()) { if(m_ManageMemory) delete [] m_Data; } }
mitkIpFloat8_t mitkIpFuncVarR ( mitkIpPicDescriptor *pic_old, mitkIpUInt4_t *begin, mitkIpUInt4_t *length ) { mitkIpFloat8_t var; /* variance */ mitkIpPicDescriptor *pic_help; pic_help = mitkIpFuncWindow ( pic_old, begin, length ); var = mitkIpFuncVar ( pic_help ); mitkIpPicFree ( pic_help ); return ( var ); }
mitkIpPicDescriptor * MITKipPicGet( char *infile_name, mitkIpPicDescriptor *pic ) { if(pic != NULL) { mitkIpPicDescriptor * tmpPic; size_t sizePic, sizeTmpPic; tmpPic = mitkIpPicGet(infile_name, NULL); sizePic = _mitkIpPicSize(pic); sizeTmpPic = _mitkIpPicSize(tmpPic); if(sizePic != sizeTmpPic) { fprintf( stderr, "Critical: MITKipPicGet was given a pic with wrong size\n" ); return tmpPic; } memcpy(pic->data, tmpPic->data, sizePic); pic->info->tags_head = tmpPic->info->tags_head; tmpPic->info->tags_head = NULL; mitkIpPicFree(tmpPic); return pic; } else return mitkIpPicGet(infile_name, pic); }
mitk::Image::Pointer mitk::PicFileReader::CreateImage() { Image::Pointer output = Image::New(); std::string fileName = this->GetLocalFileName(); mitkIpPicDescriptor *header = mitkIpPicGetHeader(const_cast<char *>(fileName.c_str()), NULL); if (!header) { mitkThrow() << "File could not be read."; } header = mitkIpPicGetTags(const_cast<char *>(fileName.c_str()), header); int channels = 1; mitkIpPicTSV_t *tsv; if ((tsv = mitkIpPicQueryTag(header, "SOURCE HEADER")) != NULL) { if (tsv->n[0] > 1e+06) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag(header, "SOURCE HEADER"); mitkIpPicFreeTag(tsvSH); } } if ((tsv = mitkIpPicQueryTag(header, "ICON80x80")) != NULL) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag(header, "ICON80x80"); mitkIpPicFreeTag(tsvSH); } if ((tsv = mitkIpPicQueryTag(header, "VELOCITY")) != NULL) { ++channels; mitkIpPicDelTag(header, "VELOCITY"); } if (header == NULL || header->bpe == 0) { mitkThrow() << " Could not read file " << fileName; } // if pic image only 2D, the n[2] value is not initialized unsigned int slices = 1; if (header->dim == 2) { header->n[2] = slices; } // First initialize the geometry of the output image by the pic-header SlicedGeometry3D::Pointer slicedGeometry = mitk::SlicedGeometry3D::New(); PicHelper::InitializeEvenlySpaced(header, header->n[2], slicedGeometry); // if pic image only 3D, the n[3] value is not initialized unsigned int timesteps = 1; if (header->dim > 3) { timesteps = header->n[3]; } slicedGeometry->ImageGeometryOn(); ProportionalTimeGeometry::Pointer timeGeometry = ProportionalTimeGeometry::New(); timeGeometry->Initialize(slicedGeometry, timesteps); // Cast the pic descriptor to ImageDescriptor and initialize the output output->Initialize(CastToImageDescriptor(header)); output->SetTimeGeometry(timeGeometry); mitkIpPicFree(header); return output; }
main( int argc, char *argv[] ) { XtAppContext app_context; Widget toplevel, bb, pic, annotation; /*Widget button;*/ toplevel = XtVaAppInitialize( &app_context, "Test", NULL, 0, &argc, argv, fallback_resources, NULL ); XtAddEventHandler( toplevel, (EventMask)0, True,_XEditResCheckMessages, NULL ); n = 0; XtSetArg( args[n], XmNheight, 400 ); n++; XtSetArg( args[n], XmNwidth, 400 ); n++; XtSetValues( toplevel, args, n ); bb = XmCreateDrawingArea( toplevel, "bb", args, n ); XtManageChild( bb ); n = 0; XtSetArg( args[n], XmNx, 200 ); n++; XtSetArg( args[n], XmNy, 100 ); n++; annotation = XtCreateManagedWidget( "annotation", xipAnnotationWidgetClass, bb, args, n ); XtAddCallback( annotation, XipNactivateCallback, activate, NULL ); n = 0; XtSetArg( args[n], XmNx, 0 ); n++; XtSetArg( args[n], XmNy, 0 ); n++; XtSetArg( args[n], XipNquantisation, True ); n++; XtSetArg( args[n], XipNpic, mitkIpPicGet( "../b.pic", NULL ) ); n++; pic = XtCreateManagedWidget( "pic", xipPicWidgetClass, bb, args, n ); /**************/ XtRealizeWidget( toplevel ); /**************/ { mitkIpPicDescriptor *pic; _mitkIpPicTagsElement_t *head; mitkIpPicTSV_t *tsv; pic = mitkIpPicGetTags( "../b.pic", NULL ); tsv = mitkIpPicQueryTag( pic, "ANNOTATION" ); if( tsv != NULL ) { head = tsv->value; tsv = _mitkIpPicFindTag( head, "TEXT" )->tsv; text = malloc( strlen(tsv->value) ); strcpy( text, tsv->value ); printf( "%s\n", text ); tsv = _mitkIpPicFindTag( head, "POSITION" )->tsv; x = ((mitkIpUInt4_t *)(tsv->value))[0]; y = ((mitkIpUInt4_t *)(tsv->value))[1]; printf( "%i %i\n", x, y ); } mitkIpPicFree( pic ); } /**************/ XtVaSetValues( annotation, XmNx, x, XmNy, y, NULL ); /**************/ XtAppMainLoop(app_context); }
bool mitk::CorrectorAlgorithm::modifySegment( int lineStart, int lineEnd, ipMITKSegmentationTYPE state, mitkIpPicDescriptor *pic, int* _ofsArray ) { // offsets for pixels right, top, left, bottom int nbDelta4[4]; nbDelta4[0]=1; nbDelta4[1]=pic->n[0]; nbDelta4[1]*=-1; // necessary because of unsigned declaration of pic->n nbDelta4[2]=-1; nbDelta4[3]=pic->n[0]; // offsets for pixels right, top-right, top, top-left left, bottom-left, bottom, bottom-right int nbDelta8[8]; nbDelta8[0] = 1; nbDelta8[1] = nbDelta4[1]+1; nbDelta8[2] = nbDelta4[1]; nbDelta8[3] = nbDelta4[1]-1; nbDelta8[4] = -1; nbDelta8[5] = nbDelta4[3]-1; nbDelta8[6] = nbDelta4[3]; nbDelta8[7] = nbDelta4[3]+1; ipMITKSegmentationTYPE* picdata = static_cast<ipMITKSegmentationTYPE*>(pic->data); ipMITKSegmentationTYPE saveStart = *(picdata + _ofsArray[lineStart]); ipMITKSegmentationTYPE saveEnd = *(picdata + _ofsArray[lineEnd]); ipMITKSegmentationTYPE newState = ((!state)&1) + 2; // probably equal to: ipMITKSegmentationTYPE newState = 3 - state; // make two copies of pic: mitkIpPicDescriptor *seg1 = mitkIpPicClone( pic ); mitkIpPicDescriptor *seg2 = mitkIpPicClone( pic ); int i; // mark line in original for (i=lineStart; i<=lineEnd; i++) { *(picdata + _ofsArray[i]) = 3; } // mark the first side in copy 1: bool firstPix = true; bool modified; int line = pic->n[0]; // #pixels in line int maxOfs = (int)(line * pic->n[1]); // #pixels in slice for (i=lineStart+1; i<lineEnd; i++) { do { modified = false; for (int nb=0; nb<8; nb++) { int nbOfs = _ofsArray[i] + nbDelta8[nb]; if ( nbOfs < 0 // above first line || nbOfs >= maxOfs // below last line ) continue; ipMITKSegmentationTYPE nbVal = *(picdata + nbOfs); ipMITKSegmentationTYPE destVal = *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs); if (nbVal!=3 && destVal!=newState) { // get only neigbhours that are not part of the line itself if (firstPix) { *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs) = newState; // this one is used to mark the side! firstPix = false; modified = true; } else { int tnb = 0; while ( tnb < 4 && ((nbOfs + nbDelta4[tnb]) >= 0) && ((nbOfs + nbDelta4[tnb]) < maxOfs) && *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs + nbDelta4[tnb]) != newState ) tnb++; if (tnb < 4 && ((nbOfs + nbDelta4[tnb]) >= 0) && ((nbOfs + nbDelta4[tnb]) < maxOfs) ) { *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs) = newState; // we've got a buddy close modified = true; } } } } } while (modified); } // mark the other side in copy 2: for (i=lineStart+1; i<lineEnd; i++) { for (int nb=0; nb<4; nb++) { int nbOfs = _ofsArray[i] + nbDelta4[nb]; if ( nbOfs < 0 // above first line || nbOfs >= maxOfs // below last line ) continue; ipMITKSegmentationTYPE lineVal = *(picdata + nbOfs); ipMITKSegmentationTYPE side1Val = *(((ipMITKSegmentationTYPE*)seg1->data) + nbOfs); if (lineVal != 3 && side1Val != newState) { *(((ipMITKSegmentationTYPE*)seg2->data) + nbOfs) = newState; } } } // take care of line ends for multiple segments: *(((ipMITKSegmentationTYPE*)seg1->data) + _ofsArray[lineStart]) = newState; *(((ipMITKSegmentationTYPE*)seg1->data) + _ofsArray[lineEnd]) = newState; *(((ipMITKSegmentationTYPE*)seg2->data) + _ofsArray[lineStart]) = newState; *(((ipMITKSegmentationTYPE*)seg2->data) + _ofsArray[lineEnd]) = newState; // replace regions: newState = (!state)&1; int sizeRegion1 = 0, sizeRegion2 = 0; for (i=lineStart+1; i<lineEnd; i++) { if (*(((ipMITKSegmentationTYPE*)seg1->data) + _ofsArray[i]) != newState) { sizeRegion1 += ipMITKSegmentationReplaceRegion4N( seg1, _ofsArray[i], newState ); } if (*(((ipMITKSegmentationTYPE*)seg2->data) + _ofsArray[i]) != newState) { sizeRegion2 += ipMITKSegmentationReplaceRegion4N( seg2, _ofsArray[i], newState ); } } // combine image: //printf( "Size Region1 = %8i Size Region2 = %8i\n", sizeRegion1, sizeRegion2 ); int sizeDif; ipMITKSegmentationTYPE *current, *segSrc; if (sizeRegion1 < sizeRegion2) { segSrc = (ipMITKSegmentationTYPE*)seg1->data; sizeDif = sizeRegion2 - sizeRegion1; } else { segSrc = (ipMITKSegmentationTYPE*)seg2->data; sizeDif = sizeRegion1 - sizeRegion2; } modified = false; if (sizeDif > 2*(lineEnd-lineStart)) { // decision is safe enough: ipMITKSegmentationTYPE *end = picdata + (pic->n[0]*pic->n[1]); for (current = picdata; current<end; current++) { if (*segSrc == newState) *current = newState; segSrc++; } modified = true; } // restore line: for (int i=lineStart+1; i<lineEnd; i++) { *(picdata + _ofsArray[i]) = state; } // restore end points: *(picdata + _ofsArray[lineStart]) = saveStart; *(picdata + _ofsArray[lineEnd]) = saveEnd; mitkIpPicFree( seg1 ); mitkIpPicFree( seg2 ); return modified; }
void ipMITKSegmentationFree (mitkIpPicDescriptor* segmentation) { if (!segmentation) return; mitkIpPicFree (segmentation); }
mitkIpPicDescriptor *mitkIpFuncScale ( mitkIpPicDescriptor *pic_old, mitkIpFloat8_t sc_fact[_mitkIpPicNDIM], mitkIpFuncFlagI_t sc_kind ) { mitkIpUInt4_t i; /* loop index */ mitkIpPicDescriptor *pic_new; /* pointer to scaled image */ char is_color=0; /* check whether data are correct */ if(pic_old->bpe==24) { pic_old->bpe=8; is_color=1; } if ( _mitkIpFuncError ( pic_old ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); if(is_color) pic_old->bpe=24; /* allocate pic_new */ pic_new = mitkIpPicCopyHeader ( pic_old, NULL ); if ( pic_new == NULL ) { _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR ); return ( mitkIpFuncERROR ); } /* copy scaling information to header of pic_new */ pic_new->dim = pic_old->dim; for ( i = 0; i < pic_old->dim; i++ ) pic_new->n[i] = ( sc_fact[i] == 0 ) ? pic_old->n[i] : ( mitkIpUInt4_t ) ( sc_fact[i] * pic_old->n[i] ); /* call scaling routines */ if ( sc_kind == mitkIpFuncScaleNN ) pic_new = _mitkIpFuncScNN ( pic_old, pic_new ); else if ( sc_kind == mitkIpFuncScaleBl ) { pic_new = _mitkIpFuncScBL ( pic_old, pic_new ); /*printf("using NN due to error in _mitkIpFuncScaleBL ... "); pic_new = _mitkIpFuncScNN ( pic_old, pic_new );*/ } else { mitkIpPicFree ( pic_new ); _mitkIpFuncSetErrno ( mitkIpFuncFLAG_ERROR ); return ( mitkIpFuncERROR ); } /* Copy Tags */ mitkIpFuncCopyTags(pic_new, pic_old); return( pic_new ); }
mitkIpPicDescriptor *mitkIpFuncSqrt ( mitkIpPicDescriptor *pic_1, mitkIpFuncFlagI_t keep, mitkIpPicDescriptor *pic_return ) { mitkIpPicDescriptor *pic_new; /* pointer to new image */ mitkIpUInt4_t i; /* loop index */ mitkIpFloat8_t min1, max1; /* extreme greyvalues of 1. image */ /* check image data */ if ( _mitkIpFuncError ( pic_1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); /* calculate max. and min. greyvalues of both images */ if ( mitkIpFuncExtr ( pic_1, &min1, &max1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); /* calculate max. and min. possible greyvalues for data type of images*/ /* find out data type of new iamge */ if ( keep == mitkIpFuncKeep ) { pic_new = _mitkIpFuncMalloc ( pic_1, pic_return, mitkIpOVERWRITE ); if ( pic_new == NULL ) return ( mitkIpFuncERROR ); } else if ( keep == mitkIpFuncNoKeep ) { pic_new = mitkIpPicNew (); pic_new->dim = pic_1->dim; pic_new->bpe = 64; pic_new->type = mitkIpPicFloat; for ( i = 0; i < pic_new->dim; i++ ) pic_new->n[i] = pic_1->n[i]; } else { _mitkIpFuncSetErrno ( mitkIpFuncFLAG_ERROR ); return ( mitkIpFuncERROR ); } if ( pic_new == NULL ) { _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR ); return ( mitkIpFuncERROR ); } if ( keep == mitkIpFuncNoKeep ) pic_new->data = malloc ( _mitkIpPicSize ( pic_new ) ); if ( pic_new->data == NULL ) { mitkIpPicFree ( pic_new ); _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR ); return ( mitkIpFuncERROR ); } /* macro to invert the picture (for all data types) */ mitkIpPicFORALL_1 ( SQRT1, pic_1, pic_new ) /* Copy Tags */ mitkIpFuncCopyTags(pic_new, pic_1); return pic_new; }
void mitk::PicFileReader::GenerateData() { Image::Pointer output = this->GetOutput(); // Check to see if we can read the file given the name or prefix // if ( m_FileName == "" && m_FilePrefix == "" ) { throw itk::ImageFileReaderException(__FILE__, __LINE__, "One of FileName or FilePrefix must be non-empty"); } if( m_FileName != "") { mitkIpPicDescriptor* outputPic = mitkIpPicNew(); outputPic = CastToIpPicDescriptor(output, outputPic); mitkIpPicDescriptor* pic=MITKipPicGet(const_cast<char *>(m_FileName.c_str()), outputPic); // comes upside-down (in MITK coordinates) from PIC file ConvertHandedness(pic); mitkIpPicTSV_t *tsv; if ( (tsv = mitkIpPicQueryTag( pic, "SOURCE HEADER" )) != NULL) { if(tsv->n[0]>1e+06) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag( pic, "SOURCE HEADER" ); mitkIpPicFreeTag(tsvSH); } } if ( (tsv = mitkIpPicQueryTag( pic, "ICON80x80" )) != NULL) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag( pic, "ICON80x80" ); mitkIpPicFreeTag(tsvSH); } if ( (tsv = mitkIpPicQueryTag( pic, "VELOCITY" )) != NULL) { mitkIpPicDescriptor* header = mitkIpPicCopyHeader(pic, NULL); header->data = tsv->value; ConvertHandedness(header); output->SetChannel(header->data, 1); header->data = NULL; mitkIpPicFree(header); mitkIpPicDelTag( pic, "VELOCITY" ); } //slice-wise reading //currently much too slow. //else //{ // int sstart, smax; // int tstart, tmax; // sstart=output->GetRequestedRegion().GetIndex(2); // smax=sstart+output->GetRequestedRegion().GetSize(2); // tstart=output->GetRequestedRegion().GetIndex(3); // tmax=tstart+output->GetRequestedRegion().GetSize(3); // int s,t; // for(s=sstart; s<smax; ++s) // { // for(t=tstart; t<tmax; ++t) // { // mitkIpPicDescriptor* pic=mitkIpPicGetSlice(const_cast<char *>(m_FileName.c_str()), NULL, t*smax+s+1); // output->SetPicSlice(pic,s,t); // } // } //} } else { int position; mitkIpPicDescriptor* pic=NULL; int zDim=(output->GetDimension()>2?output->GetDimensions()[2]:1); printf("\n zdim is %u \n",zDim); for (position = 0; position < zDim; ++position) { char fullName[1024]; sprintf(fullName, m_FilePattern.c_str(), m_FilePrefix.c_str(), m_StartFileIndex+position); pic=MITKipPicGet(fullName, pic); if(pic==NULL) { itkDebugMacro("Pic file '" << fullName << "' does not exist."); } /* FIXME else if(output->SetPicSlice(pic, position)==false) { itkDebugMacro("Image '" << fullName << "' could not be added to Image."); }*/ } if(pic!=NULL) mitkIpPicFree(pic); } }
mitkIpPicDescriptor *mitkIpFuncGausF ( mitkIpPicDescriptor *pic_old, mitkIpUInt4_t len_mask, mitkIpUInt4_t dim_mask, mitkIpFuncFlagI_t border ) { mitkIpPicDescriptor *pic_new; /* pointer to new image structure */ mitkIpPicDescriptor *pic_mask; /* pointer to mask */ mitkIpUInt4_t n[_mitkIpPicNDIM]; /* size of each dimension */ mitkIpUInt4_t ind[_mitkIpPicNDIM]; /* loop index vector */ mitkIpUInt4_t i, k; /* loop index */ mitkIpUInt4_t no_elem; /* number of mask elements */ mitkIpUInt4_t offset; /* offset of pixels */ mitkIpUInt4_t element; /* used to calculate mask elements */ mitkIpUInt4_t sum; /* sum of all mask elements */ mitkIpUInt4_t nn, nfac, kfac; /* used to calculate bin. coeff */ mitkIpUInt4_t *bin; /* binomial coeffizients */ /* check data */ if ( _mitkIpFuncError ( pic_old ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); if ( pic_old->dim < dim_mask || dim_mask < 1 ) { _mitkIpFuncSetErrno ( mitkIpFuncDIMMASC_ERROR ); return ( mitkIpFuncERROR ); } if ( len_mask % 2 != 1 ) { _mitkIpFuncSetErrno ( mitkIpFuncDIM_ERROR ); return ( mitkIpFuncERROR ); } /* calculate binomial coefficient */ bin = malloc ( len_mask * sizeof ( mitkIpUInt4_t ) ); if ( bin == NULL ) { _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR ); return ( mitkIpFuncERROR ); } nn = len_mask; bin[0] = 1; bin[nn-1] = 1; nfac = 1; kfac = 1; for ( k = 1; k < nn-1; k++ ) { kfac = k * kfac; nfac = nfac * ( nn - k ); bin[k] = nfac / kfac; } /* initialize mask */ pic_mask = mitkIpPicNew(); if ( pic_mask == NULL ) { free ( bin ); _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR ); return ( mitkIpFuncERROR ); } pic_mask->type = mitkIpPicFloat; pic_mask->bpe = 64; pic_mask->dim = dim_mask; for ( i = 0; i < dim_mask; i++ ) pic_mask->n[i] = len_mask; pic_mask->data = malloc ( _mitkIpPicSize ( pic_mask ) ); if ( pic_mask->data == NULL ) { free ( bin ); mitkIpPicFree ( pic_mask ); _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR ); return ( mitkIpFuncERROR ); } /* initialize vectors */ for ( i = 0; i < pic_mask->dim; i++ ) n[i] = len_mask; for ( i = pic_mask->dim; i < _mitkIpPicNDIM; i++ ) n[i] = 1; /* calculate mask */ offset = 0; sum = 0; for ( ind[7] = 0; ind[7] < n[7]; ind[7]++ ) for ( ind[6] = 0; ind[6] < n[6]; ind[6]++ ) for ( ind[5] = 0; ind[5] < n[5]; ind[5]++ ) for ( ind[4] = 0; ind[4] < n[4]; ind[4]++ ) for ( ind[3] = 0; ind[3] < n[3]; ind[3]++ ) for ( ind[2] = 0; ind[2] < n[2]; ind[2]++ ) for ( ind[1] = 0; ind[1] < n[1]; ind[1]++ ) for ( ind[0] = 0; ind[0] < n[0]; ind[0]++ ) { element = 1; for ( i = 0; i < pic_mask->dim; i++ ) element = element * bin[ind[i]]; (( mitkIpFloat8_t * )pic_mask->data)[offset] = ( mitkIpFloat8_t ) element; sum = sum + element; offset++; } no_elem = _mitkIpPicElements ( pic_mask ); for ( i = 0; i < no_elem; i++ ) (( mitkIpFloat8_t * ) pic_mask->data ) [i] = (( mitkIpFloat8_t * ) pic_mask->data ) [i] / ( mitkIpFloat8_t ) sum; /* convolve image with Gausian mask */ pic_new = mitkIpFuncConv ( pic_old, pic_mask, border ); mitkIpPicFree ( pic_mask ); free ( bin ); /* Copy Tags */ mitkIpFuncCopyTags(pic_new, pic_old); return ( pic_new ); }
void mitkIpPicPutSlice( const char *outfile_name, mitkIpPicDescriptor *pic, mitkIpUInt4_t slice ) { mitkIpPicDescriptor *pic_in; FILE *outfile; size_t ignored; pic_in = mitkIpPicGetHeader( outfile_name, NULL ); if( pic_in == NULL ) { if( slice == 1 ) { mitkIpBool_t compression; pic->n[pic->dim] = 1; pic->dim += 1; compression = mitkIpPicSetWriteCompression( mitkIpFalse ); mitkIpPicPut( outfile_name, pic ); mitkIpPicSetWriteCompression( compression ); pic->dim -= 1; pic->n[pic->dim] = 0; return; } else return; } pic_in = mitkIpPicGetTags( outfile_name, pic_in ); outfile = fopen( outfile_name, "r+b" ); if( outfile == NULL ) { /*ipPrintErr( "mitkIpPicPut: sorry, error opening outfile\n" );*/ /*return();*/ } if( pic->dim != pic_in->dim - 1 ) { fclose( outfile ); return; } else if( pic->n[0] != pic_in->n[0] ) { fclose( outfile ); return; } else if( pic->n[1] != pic_in->n[1] ) { fclose( outfile ); return; } if( slice > pic_in->n[pic_in->dim-1] ) pic_in->n[pic_in->dim-1] += 1; /* write outfile */ /*fseek( outfile, 0, SEEK_SET );*/ rewind( outfile ); ignored = fwrite( mitkIpPicVERSION, 1, sizeof(mitkIpPicTag_t), outfile ); fseek( outfile, sizeof(mitkIpUInt4_t), SEEK_CUR ); /* skip tags_len */ ignored = mitkIpFWriteLE( &(pic_in->type), sizeof(mitkIpUInt4_t), 1, outfile ); ignored = mitkIpFWriteLE( &(pic_in->bpe), sizeof(mitkIpUInt4_t), 1, outfile ); ignored = mitkIpFWriteLE( &(pic_in->dim), sizeof(mitkIpUInt4_t), 1, outfile ); ignored = mitkIpFWriteLE( pic_in->n, sizeof(mitkIpUInt4_t), pic_in->dim, outfile ); fseek( outfile, pic_in->info->pixel_start_in_file + _mitkIpPicSize(pic) * (slice - 1), SEEK_SET ); ignored = mitkIpFWriteLE( pic->data, pic->bpe / 8, _mitkIpPicElements(pic), outfile ); /*fseek( outfile, 0, SEEK_END );*/ fclose( outfile ); mitkIpPicFree(pic_in); /*return();*/ }
mitkIpPicDescriptor* ipMITKSegmentationCreateGrowerHistory( mitkIpPicDescriptor *seg, int startOfs, mitkIpPicDescriptor *histBuffer ) { std::queue<int> ofsQueue; if (!seg) return 0; if (!histBuffer) { histBuffer = mitkIpPicCopyHeader( seg, histBuffer ); histBuffer->type = mitkIpPicUInt; histBuffer->bpe = 16; mitkIpUInt4_t size = _mitkIpPicSize( histBuffer ); histBuffer->data = malloc( size ); memset( histBuffer->data, 0, size ); // clear buffer } else { // check if dimension of histBuffer is valid, if not: change it! if (histBuffer->n[0] != seg->n[0] || histBuffer->n[1] != seg->n[1]) { histBuffer->n[0] = seg->n[0]; histBuffer->n[1] = seg->n[1]; mitkIpUInt4_t size = _mitkIpPicSize( histBuffer ); histBuffer->data = realloc( histBuffer->data, size ); if (histBuffer->data == 0) return 0; memset( histBuffer->data, 0, size ); // clear buffer } } // create a clear buffer to check wether a point has already been visited // (seg cannot be modifier and histBuffer can contain any value) mitkIpPicDescriptor *flagBuffer = mitkIpPicCopyHeader( seg, 0 ); mitkIpUInt4_t size = _mitkIpPicSize( flagBuffer ); flagBuffer->data = malloc( size ); memset( flagBuffer->data, 0, size ); *((mitkIpUInt1_t*)flagBuffer->data+startOfs) = 1; // flag pixel as visited int line = seg->n[0]; int maxOfs = (int)(line * seg->n[1]); int testOfs; mitkIpUInt1_t segVal, flagVal; int iteration = 0; int currentWave = 1; int nextWave = 0; ofsQueue.push( startOfs ); while (!ofsQueue.empty()) { int nextOfs = ofsQueue.front(); ofsQueue.pop(); currentWave--; *((mitkIpUInt2_t*)histBuffer->data+nextOfs) = (mitkIpUInt2_t)(iteration+1); // seed point should get history = 1 // check right: testOfs = nextOfs+1; if (testOfs%line!=0) { segVal = *((mitkIpUInt1_t*)seg->data+testOfs); flagVal = *((mitkIpUInt1_t*)flagBuffer->data+testOfs); if ( segVal != 0 && flagVal == 0) { ofsQueue.push( testOfs ); *((mitkIpUInt1_t*)flagBuffer->data+testOfs) = 1; // flag pixel as visited nextWave++; } } // check top: testOfs = nextOfs-line; if (testOfs > 0) { segVal = *((mitkIpUInt1_t*)seg->data+testOfs); flagVal = *((mitkIpUInt1_t*)flagBuffer->data+testOfs); if ( segVal != 0 && flagVal == 0) { ofsQueue.push( testOfs ); *((mitkIpUInt1_t*)flagBuffer->data+testOfs) = 1; // flag pixel as visited nextWave++; } } // check left: testOfs = nextOfs-1; if (nextOfs%line!=0) { segVal = *((mitkIpUInt1_t*)seg->data+testOfs); flagVal = *((mitkIpUInt1_t*)flagBuffer->data+testOfs); if ( segVal != 0 && flagVal == 0) { ofsQueue.push( testOfs ); *((mitkIpUInt1_t*)flagBuffer->data+testOfs) = 1; // flag pixel as visited nextWave++; } } // check bottom: testOfs = nextOfs+line; if (testOfs < maxOfs) { segVal = *((mitkIpUInt1_t*)seg->data+testOfs); flagVal = *((mitkIpUInt1_t*)flagBuffer->data+testOfs); if ( segVal != 0 && flagVal == 0) { ofsQueue.push( testOfs ); *((mitkIpUInt1_t*)flagBuffer->data+testOfs) = 1; // flag pixel as visited nextWave++; } } // check for number of iterations: if (currentWave == 0) { currentWave = nextWave; nextWave = 0; iteration++; } } mitkIpPicFree( flagBuffer ); return histBuffer; }
int mitkImageTest(int argc, char* argv[]) { MITK_TEST_BEGIN(mitkImageTest); //Create Image out of nowhere mitk::Image::Pointer imgMem; mitk::PixelType pt(typeid(int)); unsigned int dim[]={100,100,20}; std::cout << "Testing creation of Image: "; imgMem=mitk::Image::New(); if(imgMem.IsNull()) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing Initialize(const mitk::PixelType& type, unsigned int dimension, unsigned int *dimensions): "; imgMem->Initialize(mitk::PixelType(typeid(int)), 3, dim); std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing IsInitialized(): "; if(imgMem->IsInitialized()==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing GetData(): "; int *p = (int*)imgMem->GetData(); if(p==NULL) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Filling image: "; unsigned int i; unsigned int size = dim[0]*dim[1]*dim[2]; for(i=0; i<size; ++i, ++p) *p= (signed int)i; std::cout<<"[PASSED]"<<std::endl; std::cout << "Getting it again and compare with filled values: "; int *p2 = (int*)imgMem->GetData(); if(p2==NULL) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } for(i=0; i<size; ++i, ++p2) { if(*p2!= (signed int)i ) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing IsInitialized(): "; if(imgMem->IsInitialized()==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing GetSliceData() and compare with filled values: "; p2 = (int*)imgMem->GetSliceData(dim[2]/2)->GetData(); unsigned int xy_size = dim[0]*dim[1]; unsigned int start_mid_slice = (dim[2]/2)*xy_size; for(i=0; i<xy_size; ++i, ++p2) { if(*p2!=(signed int)(i+start_mid_slice)) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } } std::cout<<"[PASSED]"<<std::endl; //---- mitkIpPicDescriptor *pic_slice=mitkIpPicClone(imgMem->GetSliceData(dim[2]/2)->GetPicDescriptor()); imgMem=mitk::Image::New(); std::cout << "Testing reinitializing via Initialize(const mitk::PixelType& type, unsigned int dimension, unsigned int *dimensions): "; imgMem->Initialize(mitk::PixelType(typeid(int)), 3, dim); std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing slice-wise filling via SetPicSlice(): "; for(i=0;i<dim[2];++i) { imgMem->SetPicSlice(pic_slice, i, 0, 0); } std::cout<<"[PASSED]"<<std::endl; std::cout << "Getting it again and compare with filled values: "; p2 = (int*)imgMem->GetData(); if(p2==NULL) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } for(i=0; i<size; ++i, ++p2) { if(*p2!= (signed int)((i%xy_size)+start_mid_slice)) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing IsInitialized(): "; if(imgMem->IsInitialized()==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Setting a copy of the volume once again: "; imgMem->SetPicVolume(mitkIpPicClone(imgMem->GetVolumeData(0)->GetPicDescriptor()),0); std::cout<<"[PASSED]"<<std::endl; std::cout << "Set a slice with different content via SetPicSlice(): "; memset(pic_slice->data,0,xy_size*sizeof(int)); imgMem->SetPicSlice(pic_slice, 1); std::cout << "Getting the volume again and compare the check the changed slice: "; p2 = (int*)imgMem->GetData(); if(p2==NULL) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } p2+=xy_size; for(i=0; i<xy_size; ++i, ++p2) { if(*p2!=0) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } } std::cout<<"[PASSED]"<<std::endl; std::cout << "Setting volume again: "; imgMem->SetVolume(imgMem->GetData()); std::cout<<"[PASSED]"<<std::endl; std::cout << "Set a slice with different content via SetSlice(): "; memset(pic_slice->data,0,xy_size*sizeof(int)); imgMem->SetSlice(pic_slice->data, 0); std::cout << "Getting the volume again and compare the check the changed slice: "; p2 = (int*)imgMem->GetData(); if(p2==NULL) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } for(i=0; i<xy_size; ++i, ++p2) { if(*p2!=0) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } } std::cout<<"[PASSED]"<<std::endl; //std::cout << "Testing SetVolume(): "; //imgMem->SetVolume(data); //std::cout<<"[PASSED]"<<std::endl; mitkIpPicFree(pic_slice); //----------------- // geometry information for image mitk::Point3D origin; mitk::Vector3D right, bottom; mitk::Vector3D spacing; mitk::FillVector3D(origin, 17.0, 19.92, 7.83); mitk::FillVector3D(right, 1.0, 2.0, 3.0); mitk::FillVector3D(bottom, 0.0, -3.0, 2.0); mitk::FillVector3D(spacing, 0.78, 0.91, 2.23); std::cout << "Testing InitializeStandardPlane(rightVector, downVector, spacing): " << std::flush; mitk::PlaneGeometry::Pointer planegeometry = mitk::PlaneGeometry::New(); planegeometry->InitializeStandardPlane(100, 100, right, bottom, &spacing); planegeometry->SetOrigin(origin); std::cout << "done" << std::endl; std::cout << "Testing Initialize(const mitk::PixelType& type, const mitk::Geometry3D& geometry, unsigned int slices) with PlaneGeometry and GetData(): "; imgMem->Initialize(mitk::PixelType(typeid(int)), *planegeometry); p = (int*)imgMem->GetData(); if(p==NULL) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing Initialize(const mitk::PixelType& type, int sDim, const mitk::PlaneGeometry& geometry) and GetData(): "; imgMem->Initialize(mitk::PixelType(typeid(int)), 40, *planegeometry); p = (int*)imgMem->GetData(); if(p==NULL) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; //----------------- // testing origin information and methods std::cout << "Testing correctness of origin via GetGeometry()->GetOrigin(): "; if( mitk::Equal(imgMem->GetGeometry()->GetOrigin(), origin) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing correctness of origin via GetTimeSlicedGeometry()->GetOrigin(): "; if( mitk::Equal(imgMem->GetTimeSlicedGeometry()->GetOrigin(), origin) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; mitk::FillVector3D(origin, 37.0, 17.92, 27.83); std::cout << "Setting origin via SetOrigin(origin): "; imgMem->SetOrigin(origin); std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing correctness of changed origin via GetGeometry()->GetOrigin(): "; if( mitk::Equal(imgMem->GetGeometry()->GetOrigin(), origin) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing correctness of changed origin via GetTimeSlicedGeometry()->GetOrigin(): "; if( mitk::Equal(imgMem->GetTimeSlicedGeometry()->GetOrigin(), origin) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing correctness of changed origin via GetSlicedGeometry()->GetGeometry2D(0)->GetOrigin(): "; if( mitk::Equal(imgMem->GetSlicedGeometry()->GetGeometry2D(0)->GetOrigin(), origin) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; //----------------- // testing spacing information and methods std::cout << "Testing correctness of spacing via GetGeometry()->GetSpacing(): "; if( mitk::Equal(imgMem->GetGeometry()->GetSpacing(), spacing) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing correctness of spacing via GetTimeSlicedGeometry()->GetSpacing(): "; if( mitk::Equal(imgMem->GetTimeSlicedGeometry()->GetSpacing(), spacing) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; mitk::FillVector3D(spacing, 7.0, 0.92, 1.83); std::cout << "Setting spacing via SetSpacing(spacing): "; imgMem->SetSpacing(spacing); std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing correctness of changed spacing via GetGeometry()->GetSpacing(): "; if( mitk::Equal(imgMem->GetGeometry()->GetSpacing(), spacing) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing correctness of changed spacing via GetTimeSlicedGeometry()->GetSpacing(): "; if( mitk::Equal(imgMem->GetTimeSlicedGeometry()->GetSpacing(), spacing) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing correctness of changed spacing via GetSlicedGeometry()->GetGeometry2D(0)->GetSpacing(): "; if( mitk::Equal(imgMem->GetSlicedGeometry()->GetGeometry2D(0)->GetSpacing(), spacing) == false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; //----------------- MITK_TEST_OUTPUT(<< "Testing SetImportChannel"); mitk::Image::Pointer vecImg = mitk::Image::New(); vecImg->Initialize(*imgMem->GetPixelType().GetTypeId(), *imgMem->GetGeometry(), 2 /* #channels */, 0 /*tDim*/ ); vecImg->SetImportChannel(imgMem->GetData(), 0, mitk::Image::CopyMemory ); vecImg->SetImportChannel(imgMem->GetData(), 1, mitk::Image::CopyMemory ); std::cout<<"[PASSED]"<<std::endl; MITK_TEST_OUTPUT(<< " Testing whether IsValidSlice returns valid after SetImportChannel"); MITK_TEST_CONDITION_REQUIRED( vecImg->IsValidSlice(0,0,1) , ""); MITK_TEST_OUTPUT(<< " Testing whether CopyMemory worked"); MITK_TEST_CONDITION_REQUIRED(imgMem->GetData() != vecImg->GetData(), ""); MITK_TEST_OUTPUT(<< " Testing destruction after SetImportChannel"); vecImg = NULL; std::cout<<"[PASSED]"<<std::endl; //----------------- MITK_TEST_OUTPUT(<< "Testing initialization via vtkImageData"); MITK_TEST_OUTPUT(<< " Setting up vtkImageData"); vtkImageData* vtkimage = vtkImageData::New(); vtkimage->Initialize(); vtkimage->SetDimensions( 2, 3, 4); double vtkorigin[] = {-350,-358.203, -1363.5}; vtkimage->SetOrigin(vtkorigin); mitk::Point3D vtkoriginAsMitkPoint; mitk::vtk2itk(vtkorigin, vtkoriginAsMitkPoint); double vtkspacing[] = {1.367, 1.367, 2}; vtkimage->SetSpacing(vtkspacing); vtkimage->SetScalarType( VTK_SHORT ); vtkimage->AllocateScalars(); std::cout<<"[PASSED]"<<std::endl; MITK_TEST_OUTPUT(<< " Testing mitk::Image::Initialize(vtkImageData*, ...)"); mitk::Image::Pointer mitkByVtkImage = mitk::Image::New(); mitkByVtkImage ->Initialize(vtkimage); MITK_TEST_CONDITION_REQUIRED(mitkByVtkImage->IsInitialized(), ""); MITK_TEST_OUTPUT(<< " vtkimage->Delete"); vtkimage->Delete(); std::cout<<"[PASSED]"<<std::endl; MITK_TEST_OUTPUT(<< " Testing whether spacing has been correctly initialized from vtkImageData"); mitk::Vector3D spacing2 = mitkByVtkImage->GetGeometry()->GetSpacing(); mitk::Vector3D vtkspacingAsMitkVector; mitk::vtk2itk(vtkspacing, vtkspacingAsMitkVector); MITK_TEST_CONDITION_REQUIRED(mitk::Equal(spacing2,vtkspacingAsMitkVector), ""); MITK_TEST_OUTPUT(<< " Testing whether GetSlicedGeometry(0)->GetOrigin() has been correctly initialized from vtkImageData"); mitk::Point3D origin2 = mitkByVtkImage->GetSlicedGeometry(0)->GetOrigin(); MITK_TEST_CONDITION_REQUIRED(mitk::Equal(origin2,vtkoriginAsMitkPoint), ""); MITK_TEST_OUTPUT(<< " Testing whether GetGeometry()->GetOrigin() has been correctly initialized from vtkImageData"); origin2 = mitkByVtkImage->GetGeometry()->GetOrigin(); MITK_TEST_CONDITION_REQUIRED(mitk::Equal(origin2,vtkoriginAsMitkPoint), ""); MITK_TEST_OUTPUT(<< " Testing whether GetTimeSlicedGeometry()->GetOrigin() has been correctly initialized from vtkImageData"); origin2 = mitkByVtkImage->GetTimeSlicedGeometry()->GetOrigin(); MITK_TEST_CONDITION_REQUIRED(mitk::Equal(origin2,vtkoriginAsMitkPoint), ""); // TODO test the following initializers on channel-incorporation // void mitk::Image::Initialize(const mitk::PixelType& type, unsigned int dimension, unsigned int *dimensions, unsigned int channels) // void mitk::Image::Initialize(const mitk::PixelType& type, int sDim, const mitk::Geometry2D& geometry2d, bool flipped, unsigned int channels, int tDim ) // void mitk::Image::Initialize(const mitk::Image* image) // void mitk::Image::Initialize(const mitkIpPicDescriptor* pic, int channels, int tDim, int sDim) //mitk::Image::Pointer vecImg = mitk::Image::New(); //vecImg->Initialize(PixelType(typeid(float), 6, itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR), *imgMem->GetGeometry(), 2 /* #channels */, 0 /*tDim*/, false /*shiftBoundingBoxMinimumToZero*/ ); //vecImg->Initialize(PixelType(typeid(itk::Vector<float,6>)), *imgMem->GetGeometry(), 2 /* #channels */, 0 /*tDim*/, false /*shiftBoundingBoxMinimumToZero*/ ); // testing access by index coordinates and by world coordinates mitk::DataNode::Pointer node; mitk::DataNodeFactory::Pointer nodeReader = mitk::DataNodeFactory::New(); MITK_TEST_CONDITION_REQUIRED(argc == 2, "Check if test image is accessible!"); const std::string filename = std::string(argv[1]); try { nodeReader->SetFileName(filename); nodeReader->Update(); node = nodeReader->GetOutput(); } catch(...) { MITK_TEST_FAILED_MSG(<< "Could not read file for testing: " << filename); return NULL; } mitk::Image::Pointer image = dynamic_cast<mitk::Image*>(node->GetData()); // test by index coordinates mitk::Index3D index; mitk::FillVector3D(index, 55, 39, 50); MITK_TEST_OUTPUT(<< "Testing mitk::Image::GetPixelValueByIndex"); double val = image->GetPixelValueByIndex(index); MITK_TEST_CONDITION_REQUIRED( mitk::Equal(val,112.22475433349609), ""); //test by world coordinates MITK_TEST_OUTPUT(<< "Testing mitk::Image::GetPixelValueByWorldCoordinate"); mitk::Point3D point; mitk::FillVector3D(point, -5.93752, 18.7199, 6.74218); val = image->GetPixelValueByWorldCoordinate(point); MITK_TEST_CONDITION_REQUIRED( mitk::Equal(val,94.456184387207031), ""); MITK_TEST_OUTPUT(<< "Convert to index and access value by mitk::Image::GetPixelValueByIndex again"); mitk::Index3D index2; image->GetGeometry()->WorldToIndex(point, index2); float val2 = image->GetPixelValueByIndex(index2); MITK_TEST_CONDITION_REQUIRED( mitk::Equal(val2,94.456184387207031), ""); //access via itk MITK_TEST_OUTPUT(<< "Test conversion to itk::Image"); typedef itk::Image<float,3> ItkFloatImage3D; ItkFloatImage3D::Pointer itkimage; mitk::CastToItkImage(image, itkimage); std::cout<<"[PASSED]"<<std::endl; MITK_TEST_OUTPUT(<< "Testing world->itk-physical->world consistency"); mitk::Point3D itkPhysicalPoint; image->GetGeometry()->WorldToItkPhysicalPoint(point, itkPhysicalPoint); mitk::Point3D backTransformedPoint; image->GetGeometry()->ItkPhysicalPointToWorld(itkPhysicalPoint, backTransformedPoint); MITK_TEST_CONDITION_REQUIRED( mitk::Equal(point,backTransformedPoint), ""); MITK_TEST_OUTPUT(<< "Compare value of pixel returned by mitk in comparison to itk"); itk::Index<3> idx; itkimage->TransformPhysicalPointToIndex(itkPhysicalPoint, idx); float valByItk = itkimage->GetPixel(idx); MITK_TEST_CONDITION_REQUIRED( mitk::Equal(valByItk,94.456184387207031), ""); mitk::Image::Pointer cloneImage = image->Clone(); MITK_TEST_CONDITION_REQUIRED(cloneImage->GetDimension() == image->GetDimension(), "Clone (testing dimension)"); MITK_TEST_CONDITION_REQUIRED(cloneImage->GetPixelType() == image->GetPixelType(), "Clone (testing pixel type)"); for (unsigned int i = 0u; i < cloneImage->GetDimension(); ++i) { MITK_TEST_CONDITION_REQUIRED(cloneImage->GetDimension(i) == image->GetDimension(i), "Clone (testing dimension " << i << ")"); } MITK_TEST_END(); return EXIT_SUCCESS; }
void mitk::CylindricToCartesianFilter::GenerateData() { mitk::Image::ConstPointer input = this->GetInput(); mitk::Image::Pointer output = this->GetOutput(); mitk::ImageTimeSelector::Pointer timeSelector=mitk::ImageTimeSelector::New(); timeSelector->SetInput(input); mitkIpPicDescriptor* pic_transformed=NULL; pic_transformed = mitkIpPicNew(); pic_transformed->dim=3; pic_transformed->bpe = output->GetPixelType().GetBpe(); //pic_transformed->type = output->GetPixelType().GetType(); pic_transformed->n[0] = output->GetDimension(0); pic_transformed->n[1] = output->GetDimension(1); pic_transformed->n[2] = output->GetDimension(2); pic_transformed->data=malloc(_mitkIpPicSize(pic_transformed)); int nstart, nmax; int tstart, tmax; tstart=output->GetRequestedRegion().GetIndex(3); nstart=output->GetRequestedRegion().GetIndex(4); tmax=tstart+output->GetRequestedRegion().GetSize(3); nmax=nstart+output->GetRequestedRegion().GetSize(4); if(zt==NULL) { timeSelector->SetChannelNr(nstart); timeSelector->SetTimeNr(tstart); buildTransformShortCuts(input->GetDimension(0),input->GetDimension(1), input->GetDimension(2), output->GetDimension(0), rt_pic, phit_pic, fr_pic, fphi_pic, zt, fz); // query the line limiting the sector a=b=0; mitk::FloatProperty::Pointer prop; prop = dynamic_cast<mitk::FloatProperty*>(input->GetProperty("SECTOR LIMITING LINE SLOPE").GetPointer()); if (prop.IsNotNull() ) a = prop->GetValue(); prop = dynamic_cast<mitk::FloatProperty*>(input->GetProperty("SECTOR LIMITING LINE OFFSET").GetPointer()); if (prop.IsNotNull() ) b = prop->GetValue(); buildConeCutOffShortCut(input->GetDimension(0),input->GetDimension(1), rt_pic, fr_pic, a, b, coneCutOff_pic); // mitkIpPicPut("C:\\temp\\rt_90.pic",rt_pic); //mitkIpPicPut("C:\\temp\\coneCutOff.pic", coneCutOff_pic); } int n,t; for(n=nstart;n<nmax;++n)//output->GetNumberOfChannels();++n) { timeSelector->SetChannelNr(n); for(t=tstart;t<tmax;++t) { timeSelector->SetTimeNr(t); timeSelector->Update(); // Cast to pic descriptor for the timeSelector image mitkIpPicDescriptor* timeSelectorPic = mitkIpPicNew(); CastToIpPicDescriptor( timeSelector->GetOutput(), timeSelectorPic ); _mitkIpPicFreeTags(pic_transformed->info->tags_head); pic_transformed->info->tags_head = _mitkIpPicCloneTags(timeSelectorPic->info->tags_head); if(input->GetDimension(2)>1) { mitkIpPicTypeMultiplex9(_transform, timeSelectorPic , pic_transformed, m_OutsideValue, (float*)fr_pic->data, (float*)fphi_pic->data, fz, (short *)rt_pic->data, (unsigned int *)phit_pic->data, zt, coneCutOff_pic); // mitkIpPicPut("1trf.pic",pic_transformed); } else { mitkIpPicDescriptor *doubleSlice = mitkIpPicCopyHeader( timeSelectorPic , NULL); doubleSlice->dim=3; doubleSlice->n[2]=2; doubleSlice->data=malloc(_mitkIpPicSize(doubleSlice)); memcpy(doubleSlice->data, timeSelectorPic->data, _mitkIpPicSize(doubleSlice)/2); mitkIpPicTypeMultiplex9(_transform, doubleSlice, pic_transformed, m_OutsideValue, (float*)fr_pic->data, (float*)fphi_pic->data, fz, (short *)rt_pic->data, (unsigned int *)phit_pic->data, zt, coneCutOff_pic); mitkIpPicFree(doubleSlice); } output->SetVolume(pic_transformed->data, t, n); } } //mitkIpPicPut("outzzzzzzzz.pic",pic_transformed); mitkIpPicFree(pic_transformed); m_TimeOfHeaderInitialization.Modified(); }
mitkIpPicDescriptor *mitkIpFuncSubC ( mitkIpPicDescriptor *pic_old, mitkIpFloat8_t value, mitkIpFuncFlagI_t keep, mitkIpPicDescriptor *pic_return ) { mitkIpPicDescriptor *pic_new; /* pointer to new image */ mitkIpFloat8_t max_gv; /* max. possible greyvalue */ mitkIpFloat8_t min_gv; /* min. possible greyvalue */ mitkIpFloat8_t min1, max1; /* extreme greyvalues of 1. image */ mitkIpFloat8_t smin, smax; /* product of extreme greyvalues */ /* check image data */ if ( _mitkIpFuncError ( pic_old ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); /* check value */ if ( value == 0. ) return ( pic_old ); /* else if ( value == 0. ) { _mitkIpFuncSetErrno ( mitkIpFuncDATA_ERROR ); return ( mitkIpFuncERROR ); } */ /* calculate max. and min. possible greyvalues for data type of images*/ if ( _mitkIpFuncExtT ( pic_old->type, pic_old->bpe, &min_gv, &max_gv ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); /* find out data type of new iamge */ if ( keep == mitkIpFuncKeep ) { pic_new = _mitkIpFuncMalloc ( pic_old, pic_return, mitkIpOVERWRITE ); if ( pic_new == NULL ) return ( mitkIpFuncERROR ); } else if ( keep == mitkIpFuncNoKeep ) { /* calculate max. and min. greyvalues of both images */ if ( mitkIpFuncExtr ( pic_old, &min1, &max1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); smax = max1 - value; smin = min1 - value; /* change image type of images of type mitkIpPicInt */ if ( pic_old->type == mitkIpPicInt ) { if ( smax < max_gv && smin > min_gv ) { pic_new = mitkIpPicCopyHeader ( pic_old, NULL ); } else { pic_new = mitkIpPicCopyHeader ( pic_old, NULL ); pic_new->type = mitkIpPicFloat; pic_new->bpe = 64; _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv ); } } /* change image type of images of type mitkIpPicUInt */ else if ( pic_old->type == mitkIpPicUInt ) { if ( smax < max_gv && smin > min_gv ) { pic_new = mitkIpPicCopyHeader ( pic_old, NULL ); } else { pic_new = mitkIpPicCopyHeader ( pic_old, NULL ); pic_new->type = mitkIpPicInt; pic_new->bpe = 16; _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv ); if ( smax > max_gv || smin < min_gv ) { pic_new->type = mitkIpPicFloat; pic_new->bpe = 64; _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv ); } } } /* change image type of images of type mitkIpPicUInt */ else if ( pic_old->type == mitkIpPicFloat ) { pic_new = mitkIpPicCopyHeader ( pic_old, NULL ); } else { _mitkIpFuncSetErrno ( mitkIpFuncTYPE_ERROR ); return ( mitkIpFuncERROR ); } } else { _mitkIpFuncSetErrno ( mitkIpFuncFLAG_ERROR ); return ( mitkIpFuncERROR ); } if ( pic_new == NULL ) { _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR ); return ( mitkIpFuncERROR ); } if ( keep == mitkIpFuncNoKeep ) pic_new->data = malloc ( _mitkIpPicSize ( pic_new ) ); if ( pic_new->data == NULL ) { mitkIpPicFree ( pic_new ); _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR ); return ( mitkIpFuncERROR ); } /* macro to invert the picture (for all data types) */ if ( keep == mitkIpFuncNoKeep ) mitkIpPicFORALL_2 ( SUBC, pic_old, pic_new, value ) if ( keep == mitkIpFuncKeep ) mitkIpPicFORALL_2 ( SUBC3, pic_old, pic_new, value ) /* Copy Tags */ mitkIpFuncCopyTags(pic_new, pic_old); return pic_new; }
int mitkImageSliceSelectorTest(int argc, char* argv[]) { int slice_nr = 1; std::cout << "Loading file: "; if(argc==0) { std::cout<<"no file specified [FAILED]"<<std::endl; return EXIT_FAILURE; } mitk::Image::Pointer image = NULL; mitk::DataNodeFactory::Pointer factory = mitk::DataNodeFactory::New(); try { std::cout<<argv[1]<<std::endl; factory->SetFileName( argv[1] ); factory->Update(); if(factory->GetNumberOfOutputs()<1) { std::cout<<"file could not be loaded [FAILED]"<<std::endl; return EXIT_FAILURE; } mitk::DataNode::Pointer node = factory->GetOutput( 0 ); image = dynamic_cast<mitk::Image*>(node->GetData()); if(image.IsNull()) { std::cout<<"file not an image - test will not be applied [PASSED]"<<std::endl; std::cout<<"[TEST DONE]"<<std::endl; return EXIT_SUCCESS; } } catch ( itk::ExceptionObject & ex ) { std::cout << "Exception: " << ex << "[FAILED]" << std::endl; return EXIT_FAILURE; } if(image->GetDimension(2)<2) slice_nr = 0; //Take a slice mitk::ImageSliceSelector::Pointer slice = mitk::ImageSliceSelector::New(); slice->SetInput(image); slice->SetSliceNr(slice_nr); slice->Update(); std::cout << "Testing IsInitialized(): "; if(slice->GetOutput()->IsInitialized()==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing IsSliceSet(): "; if(slice->GetOutput()->IsSliceSet(0)==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; if(itksys::SystemTools::LowerCase(itksys::SystemTools::GetFilenameExtension(argv[1])).find(".pic")!=std::string::npos) { std::cout << "Testing whether the slice is identical with a slice loaded by mitkIpPicGetSlice:"; mitkIpPicDescriptor *picslice = mitkIpPicGetSlice(argv[1], NULL, (image->GetDimension(2)-1-slice_nr)+1); int i, size = _mitkIpPicSize(picslice); char * p1 = (char*)slice->GetPic()->data; char * p2 = (char*)picslice->data; //picslice->info->write_protect=mitkIpFalse; //mitkIpPicPut("C:\\1aaaaIPPIC.pic", picslice); //mitkIpPicPut("C:\\1aaaaSEL.pic", slice->GetPic()); for(i=0; i<size; ++i, ++p1, ++p2) { if((*p1) != (*p2)) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } } std::cout<<"[PASSED]"<<std::endl; mitkIpPicFree(picslice); } try { std::cout << "Testing another, smaller (!!) input with the same slice-selector(): "; //Use CylindricToCartesianFilter mitk::CylindricToCartesianFilter::Pointer cyl2cart = mitk::CylindricToCartesianFilter::New(); cyl2cart->SetInput(image); //the output size of this filter is smaller than the of the input!! cyl2cart->SetTargetXSize( 64 ); //Use the same slice-selector again, this time to take a slice of the filtered image //which is smaller than the one of the old input!! slice->SetInput(cyl2cart->GetOutput()); slice->SetSliceNr(1); //The requested region is still the old one, //therefore the following results in most ITK versions //in an exception! slice->Update(); //If no exception occured, check that the requested region is now //the one of the smaller image if(cyl2cart->GetOutput()->GetLargestPossibleRegion().GetSize()[0]!=64) { std::cout<<"Part 1 [FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"Part 1 (without exception) [PASSED] "; //Check that the size of the output is now the one of the smaller image if((cyl2cart->GetOutput()->GetDimensions()[0]!=64) || (cyl2cart->GetOutput()->GetDimensions()[1]!=64)) { std::cout<<"Part 1b [FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"Part 1b [PASSED] "; } catch ( itk::ExceptionObject &err) { std::cout<<"Part 1(with expected exception) ... seems to be not ITK 2.0.0 [PASSED]"<<std::endl; std::cout<<err<<std::endl; //after such an exception, we need to call ResetPipeline. slice->ResetPipeline(); } try { slice->UpdateLargestPossibleRegion(); } catch ( itk::ExceptionObject ) { std::cout<<"Part 2 [FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"Part 2 [PASSED]"<<std::endl; std::cout << "Testing IsInitialized(): "; if(slice->GetOutput()->IsInitialized()==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing IsSliceSet(): "; if(slice->GetOutput()->IsSliceSet(0)==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; if(image->GetDimension(3) > 1) { int time=image->GetDimension(3)-1; std::cout << "Testing 3D+t: Setting time to " << time << ": "; slice->SetTimeNr(time); if(slice->GetTimeNr()!=time) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing 3D+t: Updating slice: "; slice->Update(); if(slice->GetOutput()->IsInitialized()==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing 3D+t: IsSliceSet(): "; if(slice->GetOutput()->IsSliceSet(0)==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing 3D+t: First slice in reader available: "; if(image->IsSliceSet(0, time)==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; } std::cout<<"[TEST DONE]"<<std::endl; return EXIT_SUCCESS; }
void mitk::DopplerToStrainRateFilter::GenerateData() { mitk::Image::ConstPointer input = this->GetInput(); mitk::Image::Pointer output = this->GetOutput(); mitk::Point3iProperty::Pointer pointProp; pointProp = dynamic_cast<mitk::Point3iProperty*>(input->GetProperty("ORIGIN").GetPointer()); if (pointProp.IsNotNull() ) { m_Origin = pointProp->GetValue(); } MITK_INFO << "compute Strain Rate Image .... " << std::endl << " origin[0]=" << m_Origin[0] << " origin[1]=" << m_Origin[1] << " origin[2]=" << m_Origin[2] << std::endl << " distance=" << m_Distance << std::endl << " NoStrainIntervall=" << m_NoStrainInterval << std::endl; const Vector3D & spacing = input->GetSlicedGeometry()->GetSpacing(); // MITK_INFO << " in: xres=" << spacing[0] << " yres=" << spacing[1] << " zres=" << spacing[2] << std::endl; mitk::ImageTimeSelector::Pointer timeSelector=mitk::ImageTimeSelector::New(); timeSelector->SetInput(input); mitkIpPicDescriptor* picStrainRate; picStrainRate = mitkIpPicNew(); picStrainRate->dim=3; picStrainRate->bpe = output->GetPixelType().GetBpe(); //picStrainRate->type = output->GetPixelType().GetType(); picStrainRate->n[0] = output->GetDimension(0); picStrainRate->n[1] = output->GetDimension(1); picStrainRate->n[2] = output->GetDimension(2); picStrainRate->data=malloc(_mitkIpPicSize(picStrainRate)); int xDim = picStrainRate->n[0]; int yDim = picStrainRate->n[1]; int zDim = picStrainRate->n[2]; long slice_size = xDim*yDim; long vol_size = slice_size*zDim; mitkIpPicDescriptor *picDoppler; int x,y,z;//,time; // loop-counter int strainRate; // the computed Strain Rate int v1,v2; // velocity and Point p1 and p2 float alpha; // the beam-angle, angle betwen current point and beam-point float dx=0, dy=0; // projection of this->distance to x- and y-axis int x1; // a square, where the velocity v1 lies in int y1; // the points are used for interpolation int minStrainRate=128, maxStrainRate=128; int n, nmax; int t, tmax; t = output->GetRequestedRegion().GetIndex(3); n = output->GetRequestedRegion().GetIndex(4); MITK_INFO << "t = " <<t << " n = " << n << std::endl; tmax = t + output->GetRequestedRegion().GetSize(3); nmax = n + output->GetRequestedRegion().GetSize(4); MITK_INFO << "tmax = "<< tmax << " nmax = " << nmax << std::endl; if (m_Distance<1) m_Distance=1; for(;n<nmax;n++)//output->GetNumberOfChannels();++n) { timeSelector->SetChannelNr(n); MITK_INFO << "computing chanel n = " << n << std::endl; for(t=0;t<tmax;t++) { MITK_INFO << "computing time slot t = " << t << std::endl; timeSelector->SetTimeNr(t); timeSelector->Update(); // Cast to pic descriptor for the timeSelector image mitkIpPicDescriptor* timeSelectorPic = mitkIpPicNew(); CastToIpPicDescriptor( timeSelector->GetOutput(), timeSelectorPic ); _mitkIpPicFreeTags(picStrainRate->info->tags_head); picStrainRate->info->tags_head = _mitkIpPicCloneTags(timeSelectorPic->info->tags_head); //#define WRITE_ANGLE_PIC #ifdef WRITE_ANGLE_PIC mitkIpPicDescriptor *anglePic; mitkIpBool_t isAnglePicWritten = mitkIpFalse; anglePic = mitkIpPicNew(); anglePic->type = mitkIpPicInt; anglePic->bpe = 8; anglePic->dim = 2; anglePic->n[0] = xDim; anglePic->n[1] = yDim; anglePic->data = (mitkIpInt1_t *)calloc(xDim*yDim,sizeof(mitkIpInt1_t)); #endif picDoppler = timeSelectorPic; picDoppler = mitkIpFuncGausF( picDoppler, 5,2 , mitkIpFuncBorderOld ) ; for (z=0 ; z<zDim ; z++) { for (y=1; y<yDim; y++) { if (y<m_Origin[1]) continue; // cannot compute StrainRate above Transducer-Position for (x=0; x<xDim; x++) { if ((m_Origin[0] - x)==0) { // winkelhalbierende int yDistanceInUnits = (int)( m_Distance/spacing[1]); int yTmp = ( (y-yDistanceInUnits)<0 ) ? 0 : (y-yDistanceInUnits); v1 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + yTmp*xDim + x] ; v2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y*xDim + x] ; } else { // compute angle to transducer position // m_Origin is given in units, not in mm alpha = atan( (float) (m_Origin[0] - x) / (float) (m_Origin[1] - y) ); // m_Distance is given in mm dx = -sin(alpha) * m_Distance; dy = -cos(alpha) * m_Distance; // convert to units dx = dx/spacing[0]; dy = dy/spacing[1]; //#define WEIGTHED #ifdef WEIGTHED weightX = dx - floor(dx); weightY = dy - floor(dy); dxFloor = (int) floor(dx); dyFloor = (int) floor(dy); x1 = x + dxFloor; // lower left y1 = y + dyFloor; x2 = x + (dxFloor+1); // lower right y2 = y + dyFloor; x3 = x + (dxFloor+1); // upper right y3 = y + (dyFloor+1); x4 = x + dxFloor; // upper left y4 = y + (dyFloor+1); vTmp1 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y1*xDim + x1]; vTmp2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y2*xDim + x2]; vTmp3 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y3*xDim + x3]; vTmp4 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y4*xDim + x4]; v1 = (int) ((1-weightX)*(1-weightY)*vTmp1 + weightX*(1-weightY)*vTmp2 + weightX*weightY*vTmp3 + (1-weightX)*weightY*vTmp4); v2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y*xDim + x] ; #else x1 = (int)(x + dx); y1 = (int)(y + dy); if (y1<0) y1=0; #endif v1 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y1*xDim + x1]; v2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y*xDim + x] ; } if (v1==128) { // schaue in Gegenrichtung, falls keine SRI hier berechenbar x1 = (int)(x - dx); y1 = (int)(y - dy); if (y1>yDim) y1=yDim; //swap v1 and v2, otherwise StrainRate is calculate in false direction v1 = v2; v2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y1*xDim + x1]; } if ( (v1==0 ) || (v2==0) || // wenn keine Geschwindigkeit vorhanden // oder wenn nur ganz kleine Geschwindigkeit vorhanden (v1>=(128-m_NoStrainInterval) && v1<=(128+m_NoStrainInterval)) || (v2>=(128-m_NoStrainInterval) && v2<=(128+m_NoStrainInterval))) { strainRate = 128; // this means no deformation // this is neccessay due to the Cyl2Cart filter } else { strainRate = (int)( (v1 - v2)/2 + 128 ); if (strainRate==128) strainRate=129; } if (strainRate < minStrainRate && strainRate > 0 ) minStrainRate = strainRate; if (strainRate > maxStrainRate) maxStrainRate = strainRate; if (strainRate<0 ) { //strainRate = -strainRate; MITK_INFO << " error: neg. strainRate ... exit() " << std::endl << " x=" << x << " y=" << y << " z=" << z << std::endl << " v1=" << v1 << " v2=" << v2 << " dist=" << m_Distance << std::endl << " sr=" << strainRate << std::endl; exit(0); } ((mitkIpUInt1_t *)picStrainRate->data)[t*vol_size + z*slice_size + y*xDim + x]=strainRate; // cout << "z: " << z << " y: " << y << " x: " << x << " strainrate: " << strainRate << endl; #ifdef WRITE_ANGLE_PIC // if (!isAnglePicWritten) // ((mitkIpInt1_t *)anglePic->data)[y*xDim + x] = (int) ( (alpha/1.6)*128); if (!isAnglePicWritten) ((mitkIpInt1_t *)anglePic->data)[y*xDim + x] = (int) ( dx); #endif } // x } // y //isAnglePicWritten = mitkIpTrue; } // z //isStrainComputed = mitkIpTrue; std::string filenameD; filenameD ="doppler.pic"; mitkIpPicPut(const_cast<char *>(filenameD.c_str()),picDoppler); #define WRITE_STRAIN_PIC #ifdef WRITE_STRAIN_PIC char tmpfilename[100]; sprintf(tmpfilename,"strain%d.pic",t);; mitkIpPicPut(tmpfilename,picStrainRate); #endif #ifdef WRITE_ANGLE_PIC std::string filename2; filename2="angle.pic"; mitkIpPicPut(const_cast<char *>(filename2.c_str()),anglePic); #endif ((mitkIpUInt1_t *)picStrainRate->data)[0]=0; ((mitkIpUInt1_t *)picStrainRate->data)[1]=255; output->SetVolume(picStrainRate->data, t, n); } } mitkIpPicFree(picStrainRate); #define WRITE_STRAIN_PIC #ifdef WRITE_STRAIN_PIC // Get the StrainRate ipPic descriptor picStrainRate = mitkIpPicNew(); CastToIpPicDescriptor( output, picStrainRate ); std::string filename; filename ="strain.pic"; mitkIpPicPut(const_cast<char *>(filename.c_str()),picStrainRate); #endif MITK_INFO << "Strain Rate Image computed.... " << std::endl << " minStrainRate: " << minStrainRate << std::endl << " maxStrainRate: " << maxStrainRate << std::endl; }
void mitk::PicFileReader::GenerateOutputInformation() { Image::Pointer output = this->GetOutput(); if ((output->IsInitialized()) && (this->GetMTime() <= m_ReadHeaderTime.GetMTime())) return; itkDebugMacro(<<"Reading file for GenerateOutputInformation()" << m_FileName); // Check to see if we can read the file given the name or prefix // if ( m_FileName == "" && m_FilePrefix == "" ) { throw itk::ImageFileReaderException(__FILE__, __LINE__, "One of FileName or FilePrefix must be non-empty"); } if( m_FileName != "") { mitkIpPicDescriptor* header=mitkIpPicGetHeader(const_cast<char *>(m_FileName.c_str()), NULL); if ( !header ) { throw itk::ImageFileReaderException(__FILE__, __LINE__, "File could not be read."); } header=MITKipPicGetTags(const_cast<char *>(m_FileName.c_str()), header); int channels = 1; mitkIpPicTSV_t *tsv; if ( (tsv = mitkIpPicQueryTag( header, "SOURCE HEADER" )) != NULL) { if(tsv->n[0]>1e+06) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag( header, "SOURCE HEADER" ); mitkIpPicFreeTag(tsvSH); } } if ( (tsv = mitkIpPicQueryTag( header, "ICON80x80" )) != NULL) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag( header, "ICON80x80" ); mitkIpPicFreeTag(tsvSH); } if ( (tsv = mitkIpPicQueryTag( header, "VELOCITY" )) != NULL) { ++channels; mitkIpPicDelTag( header, "VELOCITY" ); } if( header == NULL || header->bpe == 0) { itk::ImageFileReaderException e(__FILE__, __LINE__); itk::OStringStream msg; msg << " Could not read file " << m_FileName.c_str(); e.SetDescription(msg.str().c_str()); throw e; return; } // First initialize the geometry of the output image by the pic-header SlicedGeometry3D::Pointer slicedGeometry = mitk::SlicedGeometry3D::New(); PicHelper::InitializeEvenlySpaced(header, header->n[2], slicedGeometry); // if pic image only 3D, the n[3] value is not initialized unsigned int timesteps = 1; if( header->dim > 3 ) timesteps = header->n[3]; TimeSlicedGeometry::Pointer timeSliceGeometry = TimeSlicedGeometry::New(); timeSliceGeometry->InitializeEvenlyTimed(slicedGeometry, timesteps); timeSliceGeometry->ImageGeometryOn(); // Cast the pic descriptor to ImageDescriptor and initialize the output output->Initialize( CastToImageDescriptor(header)); output->SetGeometry( timeSliceGeometry ); mitkIpPicFree ( header ); } else { int numberOfImages=0; m_StartFileIndex=0; mitkIpPicDescriptor* header=NULL; char fullName[1024]; while(m_StartFileIndex<10) { sprintf(fullName, m_FilePattern.c_str(), m_FilePrefix.c_str(), m_StartFileIndex+numberOfImages); FILE * f=fopen(fullName,"r"); if(f==NULL) { //already found an image? if(numberOfImages>0) break; //no? let's increase start ++m_StartFileIndex; } else { fclose(f); //only open the header of the first file found, //@warning what to do when images do not have the same size?? if(header==NULL) { header=mitkIpPicGetHeader(fullName, NULL); header=MITKipPicGetTags(fullName, header); } ++numberOfImages; } } printf("\n numberofimages %d\n",numberOfImages); if(numberOfImages==0) { itk::ImageFileReaderException e(__FILE__, __LINE__); itk::OStringStream msg; msg << "no images found"; e.SetDescription(msg.str().c_str()); throw e; return; } //@FIXME: was ist, wenn die Bilder nicht alle gleich gross sind? if(numberOfImages>1) { printf("\n numberofimages %d > 1\n",numberOfImages); header->dim=3; header->n[2]=numberOfImages; } printf(" \ninitialisize output\n"); output->Initialize( CastToImageDescriptor(header) ); mitkIpPicFree ( header ); } m_ReadHeaderTime.Modified(); }
mitkIpPicDescriptor *mitkIpFuncSubI ( mitkIpPicDescriptor *pic_1, mitkIpPicDescriptor *pic_2, mitkIpFuncFlagI_t keep, mitkIpPicDescriptor *pic_return ) { mitkIpPicDescriptor *pic_new; /* pointer to new image */ mitkIpUInt4_t i; /* loop index */ mitkIpFloat8_t max_gv; /* max. possible greyvalue */ mitkIpFloat8_t min_gv; /* min. possible greyvalue */ mitkIpFloat8_t min1, max1; /* extreme greyvalues of 1. image */ mitkIpFloat8_t min2, max2; /* extreme greyvalues of 2. image */ mitkIpFloat8_t smin, smax; /* product of extreme greyvalues */ /* ckeck whether data are correct */ if ( _mitkIpFuncError ( pic_1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); if ( _mitkIpFuncError ( pic_2 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); /* check whether images have the same size */ if ( ( pic_1->type != pic_2->type ) || ( pic_1->bpe != pic_2->bpe ) ) { _mitkIpFuncSetErrno ( mitkIpFuncUNFIT_ERROR ); return NULL; } if ( pic_1->dim == pic_2->dim ) for ( i = 0; i < _mitkIpPicNDIM; i++ ) { if ( pic_1->n[i] != pic_2->n[i] ) { _mitkIpFuncSetErrno ( mitkIpFuncUNFIT_ERROR ); return NULL; } } else { _mitkIpFuncSetErrno ( mitkIpFuncUNFIT_ERROR ); return NULL; } /* calculate max. and min. possible greyvalues for data type of images*/ if ( _mitkIpFuncExtT ( pic_1->type, pic_1->bpe, &min_gv, &max_gv ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); /* find out data type of new iamge */ if ( keep == mitkIpFuncKeep ) { pic_new = _mitkIpFuncMalloc ( pic_1, pic_return, mitkIpOVERWRITE ); if ( pic_new == NULL ) return ( mitkIpFuncERROR ); } else if ( keep == mitkIpFuncNoKeep ) { /* calculate max. and min. greyvalues of both images */ if ( mitkIpFuncExtr ( pic_1, &min1, &max1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); if ( mitkIpFuncExtr ( pic_2, &min2, &max2 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR ); smax = max1 - max2; smin = min1 - min2; /* change image type of images of type mitkIpPicInt */ if ( pic_1->type == mitkIpPicInt ) { if ( smax < max_gv && smin > min_gv ) { pic_new = mitkIpPicCopyHeader ( pic_1, NULL ); } else { pic_new = mitkIpPicCopyHeader ( pic_1, NULL ); pic_new->type = mitkIpPicFloat; pic_new->bpe = 64; _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv ); } } /* change image type of images of type mitkIpPicUInt */ else if ( pic_1->type == mitkIpPicUInt ) { if ( smax < max_gv && smin > min_gv ) { pic_new = mitkIpPicCopyHeader ( pic_1, NULL ); } else { pic_new = mitkIpPicCopyHeader ( pic_1, NULL ); pic_new->type = mitkIpPicInt; pic_new->bpe = 16; _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv ); if ( smax > max_gv || smin < min_gv ) { pic_new->type = mitkIpPicFloat; pic_new->bpe = 64; _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv ); } } } /* change image type of images of type mitkIpPicUInt */ else if ( pic_1->type == mitkIpPicFloat ) { pic_new = mitkIpPicCopyHeader ( pic_1, NULL ); } else { _mitkIpFuncSetErrno ( mitkIpFuncTYPE_ERROR ); return ( mitkIpFuncERROR ); } } else { _mitkIpFuncSetErrno ( mitkIpFuncFLAG_ERROR ); return ( mitkIpFuncERROR ); } if ( pic_new == NULL ) { _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR ); return ( mitkIpFuncERROR ); } if ( keep == mitkIpFuncNoKeep ) pic_new->data = malloc ( _mitkIpPicSize ( pic_new ) ); if ( pic_new->data == NULL ) { mitkIpPicFree ( pic_new ); _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR ); return ( mitkIpFuncERROR ); } /* macro to invert the picture (for all data types) */ if ( keep == mitkIpFuncNoKeep ) mitkIpPicFORALL_2 ( SUBI, pic_1, pic_2, pic_new ) else if ( keep == mitkIpFuncKeep ) mitkIpPicFORALL_2 ( SUBI3, pic_1, pic_2, pic_new ) /* Copy Tags */ mitkIpFuncCopyTags(pic_new, pic_1); return pic_new; }