Advertisement
Guest User

Untitled

a guest
May 24th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. class MainWindow : public QWidget
  2. {
  3. Q_OBJECT
  4. public:
  5.  
  6. MainWindow(MainController * controller, StructureSensor * camera, CvCapture *openCVCam, QWidget * parent = nullptr);
  7. //.........
  8. private:
  9. //.........
  10. };
  11.  
  12. #endif // MAINWINDOW_H
  13.  
  14. MainWindow::MainWindow(MainController * controller, StructureSensor * camera, CvCapture *openCVCam, QWidget * parent)
  15. {
  16.  
  17. mUi.setupUi(this);
  18. mController = controller;
  19. mCamera = camera;
  20. mCvCapture=CvCapture;
  21. //.........
  22. }
  23.  
  24. class RGBCamera : public QWidget {
  25. private:
  26. QLabel *imagelabel;
  27. QVBoxLayout *layout;
  28.  
  29. QImage image;
  30.  
  31. public:
  32. RGBCamera(QWidget *parent = 0);
  33. ~RGBCamera(void);
  34. void putImage(IplImage *);
  35. };
  36.  
  37. #endif
  38.  
  39. #include "RGBCamera.h"
  40.  
  41. // Constructor
  42. RGBCamera::RGBCamera(QWidget *parent) : QWidget(parent) {
  43. layout = new QVBoxLayout;
  44. imagelabel = new QLabel;
  45. QImage dummy(100,100,QImage::Format_RGB32);
  46. image = dummy;
  47. layout->addWidget(imagelabel);
  48. for (int x = 0; x < 100; x ++) {
  49. for (int y =0; y < 100; y++) {
  50. image.setPixel(x,y,qRgb(x, y, y));
  51. }
  52. }
  53. imagelabel->setPixmap(QPixmap::fromImage(image));
  54.  
  55. setLayout(layout);
  56. }
  57.  
  58. RGBCamera::~RGBCamera(void) {
  59.  
  60. }
  61.  
  62. void RGBCamera::putImage(IplImage *cvimage) {
  63. int cvIndex, cvLineStart;
  64. // switch between bit depths
  65. switch (cvimage->depth) {
  66. case IPL_DEPTH_8U:
  67. switch (cvimage->nChannels) {
  68. case 3:
  69. if ( (cvimage->width != image.width()) || (cvimage->height != image.height()) ) {
  70. QImage temp(cvimage->width, cvimage->height, QImage::Format_RGB32);
  71. image = temp;
  72. }
  73. cvIndex = 0; cvLineStart = 0;
  74. for (int y = 0; y < cvimage->height; y++) {
  75. unsigned char red,green,blue;
  76. cvIndex = cvLineStart;
  77. for (int x = 0; x < cvimage->width; x++) {
  78. // DO it
  79. red = cvimage->imageData[cvIndex+2];
  80. green = cvimage->imageData[cvIndex+1];
  81. blue = cvimage->imageData[cvIndex+0];
  82.  
  83. image.setPixel(x,y,qRgb(red, green, blue));
  84. cvIndex += 3;
  85. }
  86. cvLineStart += cvimage->widthStep;
  87. }
  88. break;
  89. default:
  90. printf("This number of channels is not supportedn");
  91. break;
  92. }
  93. break;
  94. default:
  95. printf("This type of IplImage is not implemented in RGBCameran");
  96. break;
  97. }
  98. imagelabel->setPixmap(QPixmap::fromImage(image));
  99. }
  100.  
  101. class MainWindow : public QWidget
  102. {
  103. Q_OBJECT
  104. private:
  105. RGBCamera *rgbcam;
  106. CvCapture *openCVCamera;
  107.  
  108. public:
  109.  
  110. MainWindow(CvCapture *cam, QWidget *parent = 0);
  111.  
  112.  
  113. protected:
  114. void timerEvent(QTimerEvent*);
  115. };
  116.  
  117.  
  118. #endif /*MYCAMERAWINDOW_H_*/
  119.  
  120. #include "MainWindow.h"
  121.  
  122. MainWindow::MainWindow(CvCapture *openCVCam_, QWidget *parent) : QWidget(parent) {
  123. openCVCamera = openCVCam_;
  124. QVBoxLayout *layout = new QVBoxLayout;
  125. rgbcam = new RGBCamera(this);
  126. layout->addWidget(rgbcam);
  127. setLayout(layout);
  128. resize(500, 400);
  129.  
  130. startTimer(100); // 0.1-second timer
  131. }
  132.  
  133. void MainWindow::timerEvent(QTimerEvent*) {
  134. IplImage *image = cvQueryFrame(openCVCamera);
  135. rgbcam->putImage(image);
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement