28 #include <opencv\cv.h>
29 #include <opencv\cxcore.h>
30 #include <opencv\highgui.h>
31 #include <opencv2/contrib/contrib.hpp>
35 #include <itkComposeImageFilter.h>
38 #include <ISpectroCam.h>
39 #include <Jai_Factory.h>
59 class SpectroCamController_pimpl
62 SpectroCamController_pimpl();
63 ~SpectroCamController_pimpl();
66 int OpenCameraConnection();
67 int CloseCameraConnection();
68 bool isCameraRunning();
71 void SetCurrentImageAsWhiteBalance();
79 cv::Mat m_CurrentStackSmall;
80 cv::Mat m_CurrentTransformedStack;
81 cv::Mat m_FlatfieldSmall;
82 cv::Mat m_LLSQSolutionSmall;
84 const int m_FullWidth;
85 const int m_FullHeight;
87 const int m_SmallWidth;
88 const int m_SmallHeight;
90 bool m_ShowOxygenation;
93 bool m_Image1Selected;
102 bool m_IsCameraRunning;
104 unsigned m_NumRecordedImages;
106 ISpectroCam* spectroCam;
110 uint32_t m_iValidBuffers;
111 BUF_HANDLE m_pAquBufferID;
116 FastModeSettings fastSettings;
117 SequenceModeSettings seqSettings;
118 IndexModeSettings indexSettings;
120 void SaveCameraStreamToDisk();
135 if (this->m_Image1Selected)
137 this->m_Image1Selected = !this->m_Image1Selected;
138 selectedImage = this->m_CompositeItkImage_1;
142 this->m_Image1Selected = !this->m_Image1Selected;
143 selectedImage = this->m_CompositeItkImage_2;
150 this->m_CompositeMitkImage->SetVolume(selectedImage->GetBufferPointer());
151 MITK_INFO <<
"Copied data";
154 return m_CompositeMitkImage;
161 float H[8][4]= {{50104 , 37020. , 405, 1.},
162 {33209.2 , 16156.4 , 785., 1.},
163 {319.6 , 3226.56 , 280., 1.},
164 {32613.2 , 53788 , 495., 1.},
165 {26629.2 , 14550 , 760., 1.},
166 {20035.2 , 25773.6 , 665., 1.},
167 {3200 , 14677.2 , 380., 1.},
168 {290 , 1794.28 , 220., 1.}};
171 cv::Mat hMatrix = cv::Mat(8, 4, CV_32F, &H );
174 transpose(hMatrix,transH);
175 cv::Mat mulImage = transH * hMatrix;
176 cv::Mat invImage = mulImage.inv();
177 cv::Mat HCompononentsForLLSQ = invImage * transH;
179 return HCompononentsForLLSQ;
182 mitk::SpectroCamController_pimpl::SpectroCamController_pimpl()
184 m_NumRecordedImages(1),
185 m_IsCameraRunning(false),
190 m_Image1Selected(true),
191 m_ShowOxygenation(false)
194 m_CurrentStackSmall = cv::Mat(8, m_SmallWidth * m_SmallHeight, CV_32F, cv::Scalar(0));
195 m_FlatfieldSmall = cv::Mat(8, m_SmallWidth * m_SmallHeight, CV_32F, cv::Scalar(1));
196 m_CurrentTransformedStack = cv::Mat(8, m_SmallWidth * m_SmallHeight, CV_32F, cv::Scalar(1));
204 void mitk::SpectroCamController_pimpl::SetCurrentImageAsWhiteBalance()
207 m_FlatfieldSmall = m_CurrentStackSmall.clone();
209 cv::namedWindow(
"Oxygenation Estimate", WINDOW_AUTOSIZE);
210 m_ShowOxygenation =
true;
215 mitk::SpectroCamController_pimpl::~SpectroCamController_pimpl()
223 void mitk::SpectroCamController_pimpl::SaveCameraStreamToDisk()
250 bool mitk::SpectroCamController_pimpl::Ini()
254 std::ifstream fin(
"C:\\ModeSettings.txt");
255 std::ofstream fout(
"C:\\ModeSettingsCheck.txt");
256 const int bufferSize = 1000;
257 char buffer[bufferSize];
261 MITK_INFO <<
"Failed opening file ModeSettings.txt!" << endl ;
264 fin.getline(buffer, bufferSize);
265 fin.getline(buffer, bufferSize);
271 for (
int i = 0; i < 3; ++i)
273 fin.getline(buffer, bufferSize);
274 fout << buffer << endl;
280 for (
int i = 0; i < 2; ++i)
282 fin.getline(buffer, bufferSize);
283 fout << buffer << endl;
286 fin >> fastSettings.exposure;
287 fout << fastSettings.exposure << endl;;
290 for (
int i = 0; i < 3; ++i)
292 fin.getline(buffer, bufferSize);
293 fout << buffer << endl;
296 fin >> fastSettings.gain;
297 fout << fastSettings.gain << endl;
300 for (
int i = 0; i < 3; ++i)
302 fin.getline(buffer, bufferSize);
303 fout << buffer << endl;
306 for (
int i = 0; i < NUMBER_FILTERS; ++i)
308 fin >> seqSettings.exposures[i];
309 fout << seqSettings.exposures[i] << endl;
313 for (
int i = 0; i < 3; ++i)
315 fin.getline(buffer, bufferSize);
316 fout << buffer << endl;
319 for (
int i = 0; i < NUMBER_FILTERS; ++i)
321 fin >> seqSettings.gains[i];
322 fout << seqSettings.gains[i] << endl;
326 for (
int i = 0; i < 3; ++i)
328 fin.getline(buffer, bufferSize);
329 fout << buffer << endl;
332 for (
int i = 0; i < NUMBER_FILTERS; ++i)
334 fin >> indexSettings.exposures[i];
335 fout << indexSettings.exposures[i] << endl;
339 for (
int i = 0; i < 3; ++i)
341 fin.getline(buffer, bufferSize);
342 fout << buffer << endl;
344 for (
int i = 0; i < NUMBER_FILTERS; ++i)
346 fin >> indexSettings.gains[i];
347 fout << indexSettings.gains[i] << endl;
351 for (
int i = 0; i < 3; ++i)
353 fin.getline(buffer, bufferSize);
354 fout << buffer << endl;
357 for (
int i = 0; i < NUMBER_FILTERS; ++i)
359 fin >> indexSettings.numFilterIterations[i];
360 fout << indexSettings.numFilterIterations[i] << endl;
373 if (compositeImage.IsNull())
376 MITK_INFO <<
"initializing itk::Composite Image";
377 mitk::CompositeCameraImageType::RegionType region;
378 mitk::CompositeCameraImageType::RegionType::SizeType size;
379 mitk::CompositeCameraImageType::RegionType::IndexType index;
380 mitk::CompositeCameraImageType::SpacingType spacing;
382 size[0] = this->m_FullWidth;
383 size[1] = this->m_FullHeight;
386 region.SetSize(size);
387 region.SetIndex(index);
390 compositeImage->SetRegions(region);
391 compositeImage->SetSpacing(spacing);
392 compositeImage->SetNumberOfComponentsPerPixel(NUMBER_FILTERS);
393 compositeImage->Allocate();
407 if (image.m_FilterNum < 0 || image.m_FilterNum >= NUMBER_FILTERS)
409 std::cout <<
"Filter number out of range.\n"<< std::endl;
414 if (image.m_pAcqImage->pImageBuffer == NULL)
416 if (J_Image_Malloc(image.m_pAcqImage, image.m_pAcqImage) != J_ST_SUCCESS)
426 cv::Mat data = cv::Mat( image.m_pAcqImage->iSizeY, image.m_pAcqImage->iSizeX, CV_16U);
427 memcpy( data.datastart , image.m_pAcqImage->pImageBuffer , image.m_pAcqImage->iImageSize);
443 itk::ImageRegionIterator<mitk::CompositeCameraImageType> imageIterator(selectedImage, selectedImage->GetLargestPossibleRegion());
445 MatConstIterator_<unsigned short> it, end;
446 it = data.begin<
unsigned short>();
447 end = data.end<
unsigned short>();
450 while(!imageIterator.IsAtEnd())
454 compositePixel[image.m_FilterNum] = *it;
474 cv::imshow(
"Display window", display);
483 slice[0] = cv::Range( image.m_FilterNum, image.m_FilterNum+1 );
484 slice[1] = cv::Range::all();
488 cv::Mat currentImageF32;
489 display.convertTo(currentImageF32, CV_32F);
491 currentImageF32 = currentImageF32.reshape(0, 1);
492 currentImageF32.copyTo(currentSlice);
494 cv::Mat currentWorkingSlice = currentSlice.clone();
497 MITK_INFO <<
"flat current: " << currentFlatfieldSlice.at<
float>(0,100);
500 MITK_INFO <<
"raw measured pixel value: " << currentWorkingSlice.at<
float>(0,100);
502 cv::divide(currentWorkingSlice, currentFlatfieldSlice, currentWorkingSlice);
503 MITK_INFO <<
"corrected by flatfield pixel: " << currentWorkingSlice.at<
float>(0,100);
505 cv::log(currentWorkingSlice, currentWorkingSlice);
506 currentWorkingSlice = -0.43429 * currentWorkingSlice;
507 MITK_INFO <<
"to absorption: " << currentWorkingSlice.at<
float>(0,100);
514 cv::Range oxyHemo[2];
515 oxyHemo[0] = cv::Range(0,1);
516 oxyHemo[1] = cv::Range::all();
518 cv::Range deOxyHemo[2];
519 deOxyHemo[0] = cv::Range(1,2);
520 deOxyHemo[1] = cv::Range::all();
522 cv::Mat saO2 = currentEstimate(oxyHemo) / (currentEstimate(oxyHemo) + currentEstimate(deOxyHemo));
525 MITK_INFO <<
"saO2, 200 200: " << saO2Image.at<
float>(200,200);
527 cv::threshold(saO2Image, saO2Image, 1., 1., cv::THRESH_TRUNC);
528 cv::threshold(saO2Image, saO2Image, 0., 0., cv::THRESH_TOZERO);
530 saO2Image = saO2Image * 637.;
532 cv::Mat SaO2IntImage;
533 saO2Image.convertTo(SaO2IntImage, CV_8U);
537 cv::applyColorMap(SaO2IntImage, colorImage, COLORMAP_JET);
538 cv::imshow(
"Oxygenation Estimate", colorImage);
547 catch (std::exception &e) {
554 int mitk::SpectroCamController_pimpl::OpenCameraConnection()
559 MITK_INFO <<
"Camera " << model <<
" is running in: " << mode <<
"-mode" << endl;
564 J_STATUS_TYPE status = spectroCam->initialize(model.c_str(), (std::string(
"C:\\") + model +
"\\").c_str());
566 if (status != J_ST_SUCCESS)
568 MITK_INFO <<
"Could not initialize camera!" << endl;
572 this->InitializeItkImage(this->m_CompositeItkImage_1);
573 this->InitializeItkImage(this->m_CompositeItkImage_2);
580 status = status | spectroCam->start(fastSettings);
583 else if (mode ==
"Sequence")
585 status = status | spectroCam->start(seqSettings);
588 else if (mode ==
"IndexFast")
590 indexSettings.filterModeSpeed = IndexModeSettings::INDEX_FAST;
591 status = status | spectroCam->start(indexSettings);
594 else if (mode ==
"IndexSlow")
596 indexSettings.filterModeSpeed = IndexModeSettings::INDEX_SLOW;
597 status = status | spectroCam->start(indexSettings);
600 else if (mode ==
"IndexTriggered")
602 indexSettings.filterModeSpeed = IndexModeSettings::INDEX_TRIGGERED;
603 status = status | spectroCam->start(indexSettings);
608 status = status | spectroCam->start(fastSettings);
613 if (status == J_ST_SUCCESS)
615 m_IsCameraRunning =
true;
619 cv::namedWindow(
"Display window", WINDOW_AUTOSIZE );
624 bool mitk::SpectroCamController_pimpl::isCameraRunning()
626 return m_IsCameraRunning;
632 int mitk::SpectroCamController_pimpl::CloseCameraConnection()
636 J_STATUS_TYPE retval = 0;
637 CAM_HANDLE hCam = spectroCam->GetCameraHandle();
642 retval = retval | J_Camera_ExecuteCommand(hCam, (int8_t*) NODE_NAME_ACQSTOP);
651 J_DataStream_StopAcquisition(m_hDS, ACQ_STOP_FLAG_KILL);
662 J_DataStream_FlushQueue(m_hDS, ACQ_QUEUE_INPUT_TO_OUTPUT);
663 J_DataStream_FlushQueue(m_hDS, ACQ_QUEUE_OUTPUT_DISCARD);
666 J_DataStream_RevokeBuffer(m_hDS, m_pAquBufferID, &pBuffer , &pPrivate);
679 J_DataStream_Close(m_hDS);
688 retval = retval | spectroCam->stop();
701 if (J_ST_SUCCESS == retval)
703 m_IsCameraRunning =
false;
712 m_SpectroCamController_pimpl =
new SpectroCamController_pimpl();
717 delete m_SpectroCamController_pimpl;
722 return m_SpectroCamController_pimpl->OpenCameraConnection();
727 return m_SpectroCamController_pimpl->CloseCameraConnection();
732 return m_SpectroCamController_pimpl->Ini();
737 return m_SpectroCamController_pimpl->isCameraRunning();
743 return m_SpectroCamController_pimpl->GetCurrentImage();
748 m_SpectroCamController_pimpl->SetCurrentImageAsWhiteBalance();
itk::SmartPointer< Self > Pointer
void SetCurrentImageAsWhiteBalance()
itk::VectorImage< unsigned short, 2 > CompositeCameraImageType
DataCollection - Class to facilitate loading/accessing structured data.
static mitk::SpectroCamController_pimpl * my_SpectroCamController
static cv::Mat createLLSQSolutionFromHMatrix()
int OpenCameraConnection()
mitk::Image::Pointer GetCurrentImage()
static void DisplayCameraStream(SpectroCamImage image)
int CloseCameraConnection()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.