├── COPYING ├── CameraConnectDialog.cpp ├── CameraConnectDialog.h ├── CameraConnectDialog.ui ├── CaptureThread.cpp ├── CaptureThread.h ├── Config.h ├── Controller.cpp ├── Controller.h ├── FrameLabel.cpp ├── FrameLabel.h ├── ImageBuffer.cpp ├── ImageBuffer.h ├── ImageProcessingSettingsDialog.cpp ├── ImageProcessingSettingsDialog.h ├── ImageProcessingSettingsDialog.ui ├── MainWindow.cpp ├── MainWindow.h ├── MainWindow.ui ├── MatToQImage.cpp ├── MatToQImage.h ├── ProcessingThread.cpp ├── ProcessingThread.h ├── Structures.h ├── main.cpp └── qt-opencv-multithreaded.pro /COPYING: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* Nick D'Ademo */ 6 | /* */ 7 | /* Copyright (c) 2012 Nick D'Ademo */ 8 | /* */ 9 | /* Permission is hereby granted, free of charge, to any person */ 10 | /* obtaining a copy of this software and associated documentation */ 11 | /* files (the "Software"), to deal in the Software without restriction, */ 12 | /* including without limitation the rights to use, copy, modify, merge, */ 13 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 14 | /* and to permit persons to whom the Software is furnished to do so, */ 15 | /* subject to the following conditions: */ 16 | /* */ 17 | /* The above copyright notice and this permission notice shall be */ 18 | /* included in all copies or substantial portions of the Software. */ 19 | /* */ 20 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 21 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 22 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 23 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 24 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 25 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 26 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 27 | /* SOFTWARE. */ 28 | /* */ 29 | /************************************************************************/ 30 | -------------------------------------------------------------------------------- /CameraConnectDialog.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* CameraConnectDialog.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "CameraConnectDialog.h" 34 | 35 | // Qt header files 36 | #include 37 | // Configuration header file 38 | #include "Config.h" 39 | 40 | CameraConnectDialog::CameraConnectDialog(QWidget *parent) : QDialog(parent) 41 | { 42 | // Setup dialog 43 | setupUi(this); 44 | // deviceNumberEdit (device number) input string validation 45 | QRegExp rx1("[0-9]\\d{0,2}"); // Integers 0 to 999 46 | QRegExpValidator *validator1 = new QRegExpValidator(rx1, 0); 47 | deviceNumberEdit->setValidator(validator1); 48 | // imageBufferSizeEdit (image buffer size) input string validation 49 | QRegExp rx2("[0-9]\\d{0,2}"); // Integers 0 to 999 50 | QRegExpValidator *validator2 = new QRegExpValidator(rx2, 0); 51 | imageBufferSizeEdit->setValidator(validator2); 52 | // Setup combo boxes 53 | setupComboBoxes(); 54 | // Set to defaults 55 | resetToDefaults(); 56 | // Connect button to slot 57 | connect(resetToDefaultsPushButton,SIGNAL(released()),SLOT(resetToDefaults())); 58 | } // CameraConnectDialog constructor 59 | 60 | int CameraConnectDialog::getDeviceNumber() 61 | { 62 | if(cameraButtonGroup->checkedButton()==(QAbstractButton*)anyCameraButton) 63 | return -1; 64 | else 65 | { 66 | // Set device number to default (any available camera) if field is blank 67 | if(deviceNumberEdit->text().isEmpty()) 68 | { 69 | QMessageBox::warning(this->parentWidget(), "WARNING:","Device Number field blank.\nAutomatically set to 'any available camera'."); 70 | return -1; 71 | } 72 | return deviceNumberEdit->text().toInt(); 73 | } 74 | } // getDeviceNumber() 75 | 76 | int CameraConnectDialog::getImageBufferSize() 77 | { 78 | // Set image buffer size to default if field is blank 79 | if(imageBufferSizeEdit->text().isEmpty()) 80 | { 81 | QMessageBox::warning(this->parentWidget(), "WARNING:","Image Buffer Size field blank.\nAutomatically set to default value."); 82 | return DEFAULT_IMAGE_BUFFER_SIZE; 83 | } 84 | // Set image buffer size to default if field is zero 85 | else if(imageBufferSizeEdit->text().toInt()==0) 86 | { 87 | QMessageBox::warning(this->parentWidget(), "WARNING:","Image Buffer Size cannot be zero.\nAutomatically set to default value."); 88 | return DEFAULT_IMAGE_BUFFER_SIZE;; 89 | } 90 | // Use image buffer size specified by user 91 | else 92 | return imageBufferSizeEdit->text().toInt(); 93 | } // getImageBufferSize() 94 | 95 | bool CameraConnectDialog::getDropFrameCheckBoxState() 96 | { 97 | return dropFrameCheckBox->isChecked(); 98 | } // getDropFrameCheckBoxState() 99 | 100 | void CameraConnectDialog::setupComboBoxes() 101 | { 102 | // Local variables 103 | QStringList threadPriorities; 104 | // Fill combo boxes 105 | threadPriorities<<"Idle"<<"Lowest"<<"Low"<<"Normal"<<"High"<<"Highest"<<"Time Critical"<<"Inherit"; 106 | capturePrioComboBox->addItems(threadPriorities); 107 | processingPrioComboBox->addItems(threadPriorities); 108 | // Set to defaults 109 | 110 | } // setupComboBoxes() 111 | 112 | int CameraConnectDialog::getCaptureThreadPrio() 113 | { 114 | return capturePrioComboBox->currentIndex(); 115 | } // getCaptureThreadPrio() 116 | 117 | int CameraConnectDialog::getProcessingThreadPrio() 118 | { 119 | return processingPrioComboBox->currentIndex(); 120 | } // getProcessingThreadPrio() 121 | 122 | void CameraConnectDialog::resetToDefaults() 123 | { 124 | // Default camera 125 | if(DEFAULT_CAMERA_DEV_NO!=-1) 126 | { 127 | deviceNumberButton->setChecked(true); 128 | deviceNumberEdit->setEnabled(true); 129 | deviceNumberEdit->setText(QString::number(DEFAULT_CAMERA_DEV_NO)); 130 | } 131 | else 132 | { 133 | anyCameraButton->setChecked(true); 134 | deviceNumberEdit->setEnabled(false); 135 | deviceNumberEdit->clear(); 136 | } 137 | // Image buffer size 138 | imageBufferSizeEdit->setText(QString::number(DEFAULT_IMAGE_BUFFER_SIZE)); 139 | // Drop frames 140 | dropFrameCheckBox->setChecked(DEFAULT_DROP_FRAMES); 141 | // Capture thread 142 | if(DEFAULT_CAP_THREAD_PRIO==QThread::IdlePriority) 143 | capturePrioComboBox->setCurrentIndex(0); 144 | else if(DEFAULT_CAP_THREAD_PRIO==QThread::LowestPriority) 145 | capturePrioComboBox->setCurrentIndex(1); 146 | else if(DEFAULT_CAP_THREAD_PRIO==QThread::LowPriority) 147 | capturePrioComboBox->setCurrentIndex(2); 148 | else if(DEFAULT_CAP_THREAD_PRIO==QThread::NormalPriority) 149 | capturePrioComboBox->setCurrentIndex(3); 150 | else if(DEFAULT_CAP_THREAD_PRIO==QThread::HighPriority) 151 | capturePrioComboBox->setCurrentIndex(4); 152 | else if(DEFAULT_CAP_THREAD_PRIO==QThread::HighestPriority) 153 | capturePrioComboBox->setCurrentIndex(5); 154 | else if(DEFAULT_CAP_THREAD_PRIO==QThread::TimeCriticalPriority) 155 | capturePrioComboBox->setCurrentIndex(6); 156 | else if(DEFAULT_CAP_THREAD_PRIO==QThread::InheritPriority) 157 | capturePrioComboBox->setCurrentIndex(7); 158 | // Processing thread 159 | if(DEFAULT_PROC_THREAD_PRIO==QThread::IdlePriority) 160 | processingPrioComboBox->setCurrentIndex(0); 161 | else if(DEFAULT_PROC_THREAD_PRIO==QThread::LowestPriority) 162 | processingPrioComboBox->setCurrentIndex(1); 163 | else if(DEFAULT_PROC_THREAD_PRIO==QThread::LowPriority) 164 | processingPrioComboBox->setCurrentIndex(2); 165 | else if(DEFAULT_PROC_THREAD_PRIO==QThread::NormalPriority) 166 | processingPrioComboBox->setCurrentIndex(3); 167 | else if(DEFAULT_PROC_THREAD_PRIO==QThread::HighPriority) 168 | processingPrioComboBox->setCurrentIndex(4); 169 | else if(DEFAULT_PROC_THREAD_PRIO==QThread::HighestPriority) 170 | processingPrioComboBox->setCurrentIndex(5); 171 | else if(DEFAULT_PROC_THREAD_PRIO==QThread::TimeCriticalPriority) 172 | processingPrioComboBox->setCurrentIndex(6); 173 | else if(DEFAULT_PROC_THREAD_PRIO==QThread::InheritPriority) 174 | processingPrioComboBox->setCurrentIndex(7); 175 | } // resetToDefaults() 176 | -------------------------------------------------------------------------------- /CameraConnectDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* CameraConnectDialog.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef CAMERACONNECTDIALOG_H 34 | #define CAMERACONNECTDIALOG_H 35 | 36 | #include "ui_CameraConnectDialog.h" 37 | 38 | class CameraConnectDialog : public QDialog, private Ui::CameraConnectDialog 39 | { 40 | Q_OBJECT 41 | 42 | public: 43 | CameraConnectDialog(QWidget *parent = 0); 44 | void setDeviceNumber(); 45 | void setImageBufferSize(); 46 | int getDeviceNumber(); 47 | int getImageBufferSize(); 48 | bool getDropFrameCheckBoxState(); 49 | int getCaptureThreadPrio(); 50 | int getProcessingThreadPrio(); 51 | public slots: 52 | void resetToDefaults(); 53 | private: 54 | void setupComboBoxes(); 55 | }; 56 | 57 | #endif // CAMERACONNECTDIALOG_H 58 | -------------------------------------------------------------------------------- /CameraConnectDialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | CameraConnectDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 411 10 | 331 11 | 12 | 13 | 14 | 15 | 0 16 | 0 17 | 18 | 19 | 20 | 21 | 410 22 | 170 23 | 24 | 25 | 26 | Connect to Camera 27 | 28 | 29 | 30 | 31 | 11 32 | 14 33 | 391 34 | 301 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 9 45 | 75 46 | true 47 | 48 | 49 | 50 | Select Camera: 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 9 59 | 60 | 61 | 62 | Connect to any available camera 63 | 64 | 65 | true 66 | 67 | 68 | cameraButtonGroup 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 9 79 | 80 | 81 | 82 | Device Number: 83 | 84 | 85 | cameraButtonGroup 86 | 87 | 88 | 89 | 90 | 91 | 92 | false 93 | 94 | 95 | 96 | 0 97 | 0 98 | 99 | 100 | 101 | 102 | 50 103 | 0 104 | 105 | 106 | 107 | 108 | 50 109 | 16777215 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | Qt::Horizontal 118 | 119 | 120 | 121 | 40 122 | 20 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 9 134 | 75 135 | true 136 | 137 | 138 | 139 | Image Buffer: 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 9 150 | 50 151 | false 152 | 153 | 154 | 155 | Size (number of images/frames): 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 0 164 | 0 165 | 166 | 167 | 168 | 169 | 50 170 | 0 171 | 172 | 173 | 174 | 175 | 50 176 | 16777215 177 | 178 | 179 | 180 | 181 | 9 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 9 191 | 75 192 | true 193 | 194 | 195 | 196 | [1-999] 197 | 198 | 199 | 200 | 201 | 202 | 203 | Qt::Horizontal 204 | 205 | 206 | 207 | 40 208 | 20 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 9 220 | 221 | 222 | 223 | Drop frame if image buffer is full 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 9 232 | 75 233 | true 234 | 235 | 236 | 237 | Thread Priorities: 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 9 250 | 50 251 | false 252 | 253 | 254 | 255 | Capture Thread: 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 9 264 | 50 265 | false 266 | 267 | 268 | 269 | Processing Thread: 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 9 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 9 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | Qt::Horizontal 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | Reset to Defaults 314 | 315 | 316 | 317 | 318 | 319 | 320 | Qt::Horizontal 321 | 322 | 323 | QDialogButtonBox::Cancel|QDialogButtonBox::Ok 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | anyCameraButton 334 | deviceNumberButton 335 | deviceNumberEdit 336 | imageBufferSizeEdit 337 | dropFrameCheckBox 338 | capturePrioComboBox 339 | processingPrioComboBox 340 | okCancelBox 341 | resetToDefaultsPushButton 342 | 343 | 344 | 345 | 346 | okCancelBox 347 | accepted() 348 | CameraConnectDialog 349 | accept() 350 | 351 | 352 | 430 353 | 136 354 | 355 | 356 | 157 357 | 274 358 | 359 | 360 | 361 | 362 | okCancelBox 363 | rejected() 364 | CameraConnectDialog 365 | reject() 366 | 367 | 368 | 430 369 | 136 370 | 371 | 372 | 286 373 | 274 374 | 375 | 376 | 377 | 378 | anyCameraButton 379 | clicked(bool) 380 | deviceNumberEdit 381 | setDisabled(bool) 382 | 383 | 384 | 205 385 | 38 386 | 387 | 388 | 217 389 | 67 390 | 391 | 392 | 393 | 394 | deviceNumberButton 395 | clicked(bool) 396 | deviceNumberEdit 397 | setEnabled(bool) 398 | 399 | 400 | 130 401 | 93 402 | 403 | 404 | 281 405 | 93 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | -------------------------------------------------------------------------------- /CaptureThread.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* CaptureThread.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "CaptureThread.h" 34 | #include "ImageBuffer.h" 35 | 36 | // Qt header files 37 | #include 38 | // Configuration header file 39 | #include "Config.h" 40 | 41 | CaptureThread::CaptureThread(ImageBuffer *imageBuffer) : QThread(), 42 | imageBuffer(imageBuffer) 43 | { 44 | // Initialize variables 45 | stopped=false; 46 | sampleNo=0; 47 | fpsSum=0; 48 | avgFPS=0; 49 | fps.clear(); 50 | } // CaptureThread constructor 51 | 52 | void CaptureThread::run() 53 | { 54 | while(1) 55 | { 56 | ///////////////////////////////// 57 | // Stop thread if stopped=TRUE // 58 | ///////////////////////////////// 59 | stoppedMutex.lock(); 60 | if (stopped) 61 | { 62 | stopped=false; 63 | stoppedMutex.unlock(); 64 | break; 65 | } 66 | stoppedMutex.unlock(); 67 | ///////////////////////////////// 68 | ///////////////////////////////// 69 | // Save capture time 70 | captureTime=t.elapsed(); 71 | // Start timer (used to calculate capture rate) 72 | t.start(); 73 | // Capture and add frame to buffer 74 | cap>>grabbedFrame; 75 | imageBuffer->addFrame(grabbedFrame); 76 | // Update statistics 77 | updateFPS(captureTime); 78 | } 79 | qDebug() << "Stopping capture thread..."; 80 | } // run() 81 | 82 | bool CaptureThread::connectToCamera(int deviceNumber) 83 | { 84 | // Open camera and return result 85 | return cap.open(deviceNumber); 86 | } // connectToCamera() 87 | 88 | void CaptureThread::disconnectCamera() 89 | { 90 | // Check if camera is connected 91 | if(cap.isOpened()) 92 | { 93 | // Disconnect camera 94 | cap.release(); 95 | qDebug() << "Camera successfully disconnected."; 96 | } 97 | } // disconnectCamera() 98 | 99 | void CaptureThread::updateFPS(int timeElapsed) 100 | { 101 | // Add instantaneous FPS value to queue 102 | if(timeElapsed>0) 103 | { 104 | fps.enqueue((int)1000/timeElapsed); 105 | // Increment sample number 106 | sampleNo++; 107 | } 108 | // Maximum size of queue is DEFAULT_CAPTURE_FPS_STAT_QUEUE_LENGTH 109 | if(fps.size()>CAPTURE_FPS_STAT_QUEUE_LENGTH) 110 | fps.dequeue(); 111 | // Update FPS value every DEFAULT_CAPTURE_FPS_STAT_QUEUE_LENGTH samples 112 | if((fps.size()==CAPTURE_FPS_STAT_QUEUE_LENGTH)&&(sampleNo==CAPTURE_FPS_STAT_QUEUE_LENGTH)) 113 | { 114 | // Empty queue and store sum 115 | while(!fps.empty()) 116 | fpsSum+=fps.dequeue(); 117 | // Calculate average FPS 118 | avgFPS=fpsSum/CAPTURE_FPS_STAT_QUEUE_LENGTH; 119 | // Reset sum 120 | fpsSum=0; 121 | // Reset sample number 122 | sampleNo=0; 123 | } 124 | } // updateFPS() 125 | 126 | void CaptureThread::stopCaptureThread() 127 | { 128 | stoppedMutex.lock(); 129 | stopped=true; 130 | stoppedMutex.unlock(); 131 | } // stopCaptureThread() 132 | 133 | int CaptureThread::getAvgFPS() 134 | { 135 | return avgFPS; 136 | } // getAvgFPS() 137 | 138 | bool CaptureThread::isCameraConnected() 139 | { 140 | if(cap.isOpened()) 141 | return true; 142 | else 143 | return false; 144 | } // isCameraConnected() 145 | 146 | int CaptureThread::getInputSourceWidth() 147 | { 148 | return cap.get(CV_CAP_PROP_FRAME_WIDTH); 149 | } // getInputSourceWidth() 150 | 151 | int CaptureThread::getInputSourceHeight() 152 | { 153 | return cap.get(CV_CAP_PROP_FRAME_HEIGHT); 154 | } // getInputSourceHeight() 155 | -------------------------------------------------------------------------------- /CaptureThread.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* CaptureThread.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef CAPTURETHREAD_H 34 | #define CAPTURETHREAD_H 35 | 36 | // Qt header files 37 | #include 38 | #include 39 | // OpenCV header files 40 | #include "opencv/highgui.h" 41 | 42 | using namespace cv; 43 | 44 | class ImageBuffer; 45 | 46 | class CaptureThread : public QThread 47 | { 48 | Q_OBJECT 49 | 50 | public: 51 | CaptureThread(ImageBuffer *buffer); 52 | bool connectToCamera(int); 53 | void disconnectCamera(); 54 | void stopCaptureThread(); 55 | int getAvgFPS(); 56 | bool isCameraConnected(); 57 | int getInputSourceWidth(); 58 | int getInputSourceHeight(); 59 | private: 60 | void updateFPS(int); 61 | ImageBuffer *imageBuffer; 62 | VideoCapture cap; 63 | Mat grabbedFrame; 64 | QTime t; 65 | QMutex stoppedMutex; 66 | int captureTime; 67 | int avgFPS; 68 | QQueue fps; 69 | int sampleNo; 70 | int fpsSum; 71 | volatile bool stopped; 72 | protected: 73 | void run(); 74 | }; 75 | 76 | #endif // CAPTURETHREAD_H 77 | -------------------------------------------------------------------------------- /Config.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* Config.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef CONFIG_H 34 | #define CONFIG_H 35 | 36 | // OpenCV header files 37 | #include 38 | #include 39 | // Qt header files 40 | #include 41 | 42 | // FPS statistics queue lengths 43 | #define PROCESSING_FPS_STAT_QUEUE_LENGTH 32 44 | #define CAPTURE_FPS_STAT_QUEUE_LENGTH 32 45 | 46 | // Camera device number 47 | #define DEFAULT_CAMERA_DEV_NO -1 48 | // Image buffer size 49 | #define DEFAULT_IMAGE_BUFFER_SIZE 1 50 | // Drop frame if image/frame buffer is full 51 | #define DEFAULT_DROP_FRAMES false 52 | // Thread priorities 53 | #define DEFAULT_CAP_THREAD_PRIO QThread::NormalPriority 54 | #define DEFAULT_PROC_THREAD_PRIO QThread::HighPriority 55 | 56 | // IMAGE PROCESSING 57 | // Smooth 58 | #define DEFAULT_SMOOTH_TYPE 0 // Options: [BLUR=0,GAUSSIAN=1,MEDIAN=2] 59 | #define DEFAULT_SMOOTH_PARAM_1 3 60 | #define DEFAULT_SMOOTH_PARAM_2 3 61 | #define DEFAULT_SMOOTH_PARAM_3 0 62 | #define DEFAULT_SMOOTH_PARAM_4 0 63 | // Dilate 64 | #define DEFAULT_DILATE_ITERATIONS 1 65 | // Erode 66 | #define DEFAULT_ERODE_ITERATIONS 1 67 | // Flip 68 | #define DEFAULT_FLIP_CODE 0 // Options: [x-axis=0,y-axis=1,both axes=-1] 69 | // Canny 70 | #define DEFAULT_CANNY_THRESHOLD_1 10 71 | #define DEFAULT_CANNY_THRESHOLD_2 100 72 | #define DEFAULT_CANNY_APERTURE_SIZE 3 73 | #define DEFAULT_CANNY_L2GRADIENT false 74 | 75 | #endif // CONFIG_H 76 | -------------------------------------------------------------------------------- /Controller.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* Controller.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "Controller.h" 34 | #include "ImageBuffer.h" 35 | 36 | // Qt header files 37 | #include 38 | 39 | Controller::Controller() 40 | { 41 | } // Controller constructor 42 | 43 | Controller::~Controller() 44 | { 45 | } // Controller destructor 46 | 47 | bool Controller::connectToCamera(int deviceNumber, int imageBufferSize, bool dropFrame, int capThreadPrio, int procThreadPrio) 48 | { 49 | // Local variables 50 | bool isOpened=false; 51 | // Store imageBufferSize in private member 52 | this->imageBufferSize=imageBufferSize; 53 | // Create image buffer with user-defined settings 54 | imageBuffer = new ImageBuffer(imageBufferSize,dropFrame); 55 | // Create capture thread 56 | captureThread = new CaptureThread(imageBuffer); 57 | // Attempt to connect to camera 58 | if((isOpened=captureThread->connectToCamera(deviceNumber))) 59 | { 60 | // Create processing thread 61 | processingThread = new ProcessingThread(imageBuffer,captureThread->getInputSourceWidth(),captureThread->getInputSourceHeight()); 62 | // Start capturing frames from camera 63 | captureThread->start((QThread::Priority)capThreadPrio); 64 | // Start processing captured frames 65 | processingThread->start((QThread::Priority)procThreadPrio); 66 | } 67 | else 68 | { 69 | // Delete capture thread 70 | deleteCaptureThread(); 71 | // Delete image buffer 72 | deleteImageBuffer(); 73 | } 74 | // Return camera open result 75 | return isOpened; 76 | } // connectToCamera() 77 | 78 | void Controller::disconnectCamera() 79 | { 80 | // Stop processing thread 81 | if(processingThread->isRunning()) 82 | stopProcessingThread(); 83 | // Stop capture thread 84 | if(captureThread->isRunning()) 85 | stopCaptureThread(); 86 | // Clear image buffer 87 | clearImageBuffer(); 88 | // Disconnect camera 89 | captureThread->disconnectCamera(); 90 | // Delete threads 91 | deleteCaptureThread(); 92 | deleteProcessingThread(); 93 | // Delete image buffer 94 | deleteImageBuffer(); 95 | } // disconnectCamera() 96 | 97 | void Controller::stopCaptureThread() 98 | { 99 | qDebug() << "About to stop capture thread..."; 100 | captureThread->stopCaptureThread(); 101 | // Take one frame off a FULL queue to allow the capture thread to finish 102 | if(imageBuffer->getSizeOfImageBuffer()==imageBufferSize) 103 | Mat temp=imageBuffer->getFrame(); 104 | captureThread->wait(); 105 | qDebug() << "Capture thread successfully stopped."; 106 | } // stopCaptureThread() 107 | 108 | void Controller::stopProcessingThread() 109 | { 110 | qDebug() << "About to stop processing thread..."; 111 | processingThread->stopProcessingThread(); 112 | processingThread->wait(); 113 | qDebug() << "Processing thread successfully stopped."; 114 | } // stopProcessingThread() 115 | 116 | void Controller::deleteCaptureThread() 117 | { 118 | // Delete thread 119 | delete captureThread; 120 | } // deleteCaptureThread() 121 | 122 | void Controller::deleteProcessingThread() 123 | { 124 | // Delete thread 125 | delete processingThread; 126 | } // deleteProcessingThread() 127 | 128 | void Controller::clearImageBuffer() 129 | { 130 | imageBuffer->clearBuffer(); 131 | } // clearImageBuffer() 132 | 133 | void Controller::deleteImageBuffer() 134 | { 135 | // Delete image buffer 136 | delete imageBuffer; 137 | } // deleteImageBuffer() 138 | -------------------------------------------------------------------------------- /Controller.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* Controller.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef CONTROLLER_H 34 | #define CONTROLLER_H 35 | 36 | #include "CaptureThread.h" 37 | #include "ProcessingThread.h" 38 | #include "Structures.h" 39 | 40 | // Qt header files 41 | #include 42 | // OpenCV header files 43 | #include 44 | 45 | class ImageBuffer; 46 | 47 | class Controller : public QObject 48 | { 49 | Q_OBJECT 50 | 51 | public: 52 | Controller(); 53 | ~Controller(); 54 | ImageBuffer *imageBuffer; 55 | ProcessingThread *processingThread; 56 | CaptureThread *captureThread; 57 | bool connectToCamera(int,int,bool,int,int); 58 | void disconnectCamera(); 59 | void stopCaptureThread(); 60 | void stopProcessingThread(); 61 | void deleteCaptureThread(); 62 | void deleteProcessingThread(); 63 | void clearImageBuffer(); 64 | void deleteImageBuffer(); 65 | private: 66 | int imageBufferSize; 67 | }; 68 | 69 | #endif // CONTROLLER_H 70 | -------------------------------------------------------------------------------- /FrameLabel.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* FrameLabel.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "FrameLabel.h" 34 | 35 | FrameLabel::FrameLabel(QWidget *parent) : QLabel(parent) 36 | { 37 | // Initialize variables 38 | startPoint.setX(0); 39 | startPoint.setY(0); 40 | mouseCursorPos.setX(0); 41 | mouseCursorPos.setY(0); 42 | drawBox=false; 43 | // Initialize MouseData structure 44 | mouseData.leftButtonRelease=false; 45 | mouseData.rightButtonRelease=false; 46 | } // FrameLabel constructor 47 | 48 | void FrameLabel::mouseMoveEvent(QMouseEvent *ev) 49 | { 50 | // Save mouse cursor position 51 | setMouseCursorPos(ev->pos()); 52 | // Update box width and height if box drawing is in progress 53 | if(drawBox) 54 | { 55 | box->setWidth(getMouseCursorPos().x()-startPoint.x()); 56 | box->setHeight(getMouseCursorPos().y()-startPoint.y()); 57 | } 58 | // Inform main window of mouse move event 59 | emit onMouseMoveEvent(); 60 | } // mouseMoveEvent() 61 | 62 | void FrameLabel::setMouseCursorPos(QPoint input) 63 | { 64 | mouseCursorPos=input; 65 | } // setMouseCursorPos() 66 | 67 | QPoint FrameLabel::getMouseCursorPos() 68 | { 69 | return mouseCursorPos; 70 | } // getMouseXPos() 71 | 72 | void FrameLabel::mouseReleaseEvent(QMouseEvent *ev) 73 | { 74 | // Update cursor position 75 | setMouseCursorPos(ev->pos()); 76 | // On left mouse button release 77 | if(ev->button()==Qt::LeftButton) 78 | { 79 | // Set leftButtonRelease flag to TRUE 80 | mouseData.leftButtonRelease=true; 81 | if(drawBox) 82 | { 83 | // Stop drawing box 84 | drawBox=false; 85 | // Save box dimensions 86 | mouseData.selectionBox.setX(box->left()); 87 | mouseData.selectionBox.setY(box->top()); 88 | mouseData.selectionBox.setWidth(box->width()); 89 | mouseData.selectionBox.setHeight(box->height()); 90 | // Set leftButtonRelease flag to TRUE 91 | mouseData.leftButtonRelease=true; 92 | // Inform main window of event 93 | emit newMouseData(mouseData); 94 | } 95 | // Set leftButtonRelease flag to FALSE 96 | mouseData.leftButtonRelease=false; 97 | } 98 | // On right mouse button release 99 | else if(ev->button()==Qt::RightButton) 100 | { 101 | // If user presses (and then releases) the right mouse button while drawing box, stop drawing box 102 | if(drawBox) 103 | drawBox=false; 104 | else 105 | { 106 | // Set rightButtonRelease flag to TRUE 107 | mouseData.rightButtonRelease=true; 108 | // Inform main window of event 109 | emit newMouseData(mouseData); 110 | // Set rightButtonRelease flag to FALSE 111 | mouseData.rightButtonRelease=false; 112 | } 113 | } 114 | } // mouseReleaseEvent() 115 | 116 | void FrameLabel::mousePressEvent(QMouseEvent *ev) 117 | { 118 | // Update cursor position 119 | setMouseCursorPos(ev->pos());; 120 | if(ev->button()==Qt::LeftButton) 121 | { 122 | // Start drawing box 123 | startPoint=ev->pos(); 124 | box=new QRect(startPoint.x(),startPoint.y(),0,0); 125 | drawBox=true; 126 | } 127 | } // mousePressEvent() 128 | 129 | void FrameLabel::paintEvent(QPaintEvent *ev) 130 | { 131 | QLabel::paintEvent(ev); 132 | QPainter painter(this); 133 | // Draw box 134 | if(drawBox) 135 | { 136 | painter.setPen(Qt::blue); 137 | painter.drawRect(*box); 138 | } 139 | } // paintEvent() 140 | -------------------------------------------------------------------------------- /FrameLabel.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* FrameLabel.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef FRAMELABEL_H 34 | #define FRAMELABEL_H 35 | 36 | #include "Structures.h" 37 | 38 | // Qt header files 39 | #include 40 | 41 | class FrameLabel : public QLabel 42 | { 43 | Q_OBJECT 44 | 45 | public: 46 | FrameLabel(QWidget *parent = 0); 47 | void setMouseCursorPos(QPoint); 48 | QPoint getMouseCursorPos(); 49 | private: 50 | MouseData mouseData; 51 | QPoint startPoint; 52 | QPoint mouseCursorPos; 53 | bool drawBox; 54 | QRect *box; 55 | protected: 56 | void mouseMoveEvent(QMouseEvent *ev); 57 | void mousePressEvent(QMouseEvent *ev); 58 | void mouseReleaseEvent(QMouseEvent *ev); 59 | void paintEvent(QPaintEvent *ev); 60 | signals: 61 | void newMouseData(struct MouseData mouseData); 62 | void onMouseMoveEvent(); 63 | }; 64 | 65 | #endif // FRAMELABEL_H 66 | -------------------------------------------------------------------------------- /ImageBuffer.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* ImageBuffer.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "ImageBuffer.h" 34 | 35 | // Qt header files 36 | #include 37 | 38 | ImageBuffer::ImageBuffer(int bufferSize, bool dropFrame) : bufferSize(bufferSize), dropFrame(dropFrame) 39 | { 40 | // Semaphore initializations 41 | freeSlots = new QSemaphore(bufferSize); 42 | usedSlots = new QSemaphore(0); 43 | clearBuffer1 = new QSemaphore(1); 44 | clearBuffer2 = new QSemaphore(1); 45 | // Save value of dropFrame to private member 46 | this->dropFrame=dropFrame; 47 | } // ImageBuffer constructor 48 | 49 | void ImageBuffer::addFrame(const Mat& frame) 50 | { 51 | // Acquire semaphore 52 | clearBuffer1->acquire(); 53 | // If frame dropping is enabled, do not block if buffer is full 54 | if(dropFrame) 55 | { 56 | // Try and acquire semaphore to add frame 57 | if(freeSlots->tryAcquire()) 58 | { 59 | // Add frame to queue 60 | imageQueueProtect.lock(); 61 | imageQueue.enqueue(frame); 62 | imageQueueProtect.unlock(); 63 | // Release semaphore 64 | usedSlots->release(); 65 | } 66 | } 67 | // If buffer is full, wait on semaphore 68 | else 69 | { 70 | // Acquire semaphore 71 | freeSlots->acquire(); 72 | // Add frame to queue 73 | imageQueueProtect.lock(); 74 | imageQueue.enqueue(frame); 75 | imageQueueProtect.unlock(); 76 | // Release semaphore 77 | usedSlots->release(); 78 | } 79 | // Release semaphore 80 | clearBuffer1->release(); 81 | } // addFrame() 82 | 83 | Mat ImageBuffer::getFrame() 84 | { 85 | // Acquire semaphores 86 | clearBuffer2->acquire(); 87 | usedSlots->acquire(); 88 | // Temporary data 89 | Mat tempFrame; 90 | // Take frame from queue 91 | imageQueueProtect.lock(); 92 | tempFrame=imageQueue.dequeue(); 93 | imageQueueProtect.unlock(); 94 | // Release semaphores 95 | freeSlots->release(); 96 | clearBuffer2->release(); 97 | // Return frame to caller 98 | return tempFrame.clone(); 99 | } // getFrame() 100 | 101 | void ImageBuffer::clearBuffer() 102 | { 103 | // Check if buffer is not empty 104 | if(imageQueue.size()!=0) 105 | { 106 | // Stop adding frames to buffer 107 | clearBuffer1->acquire(); 108 | // Stop taking frames from buffer 109 | clearBuffer2->acquire(); 110 | // Release all remaining slots in queue 111 | freeSlots->release(imageQueue.size()); 112 | // Acquire all queue slots 113 | freeSlots->acquire(bufferSize); 114 | // Reset usedSlots to zero 115 | usedSlots->acquire(imageQueue.size()); 116 | // Clear buffer 117 | imageQueue.clear(); 118 | // Release all slots 119 | freeSlots->release(bufferSize); 120 | // Allow getFrame() to resume 121 | clearBuffer2->release(); 122 | // Allow addFrame() to resume 123 | clearBuffer1->release(); 124 | qDebug() << "Image buffer successfully cleared."; 125 | } 126 | else 127 | qDebug() << "WARNING: Could not clear image buffer: already empty."; 128 | } // clearBuffer() 129 | 130 | int ImageBuffer::getSizeOfImageBuffer() 131 | { 132 | return imageQueue.size(); 133 | } // getSizeOfImageBuffer() 134 | -------------------------------------------------------------------------------- /ImageBuffer.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* ImageBuffer.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef IMAGEBUFFER_H 34 | #define IMAGEBUFFER_H 35 | 36 | // Qt header files 37 | #include 38 | #include 39 | #include 40 | // OpenCV header files 41 | #include 42 | 43 | using namespace cv; 44 | 45 | class ImageBuffer 46 | { 47 | 48 | public: 49 | ImageBuffer(int size, bool dropFrame); 50 | void addFrame(const Mat& frame); 51 | Mat getFrame(); 52 | void clearBuffer(); 53 | int getSizeOfImageBuffer(); 54 | private: 55 | QMutex imageQueueProtect; 56 | QQueue imageQueue; 57 | QSemaphore *freeSlots; 58 | QSemaphore *usedSlots; 59 | QSemaphore *clearBuffer1; 60 | QSemaphore *clearBuffer2; 61 | int bufferSize; 62 | bool dropFrame; 63 | }; 64 | 65 | #endif // IMAGEBUFFER_H 66 | -------------------------------------------------------------------------------- /ImageProcessingSettingsDialog.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* ImageProcessingSettingsDialog.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "ImageProcessingSettingsDialog.h" 34 | 35 | // Qt header files 36 | #include 37 | // Configuration header file 38 | #include "Config.h" 39 | 40 | ImageProcessingSettingsDialog::ImageProcessingSettingsDialog(QWidget *parent) : QDialog(parent) 41 | { 42 | // Setup dialog 43 | setupUi(this); 44 | // Connect GUI signals and slots 45 | connect(resetAllToDefaultsButton,SIGNAL(released()),SLOT(resetAllDialogToDefaults())); 46 | connect(resetSmoothToDefaultsButton,SIGNAL(released()),SLOT(resetSmoothDialogToDefaults())); 47 | connect(resetDilateToDefaultsButton,SIGNAL(released()),SLOT(resetDilateDialogToDefaults())); 48 | connect(resetErodeToDefaultsButton,SIGNAL(released()),SLOT(resetErodeDialogToDefaults())); 49 | connect(resetFlipToDefaultsButton,SIGNAL(released()),SLOT(resetFlipDialogToDefaults())); 50 | connect(resetCannyToDefaultsButton,SIGNAL(released()),SLOT(resetCannyDialogToDefaults())); 51 | connect(applyButton,SIGNAL(released()),SLOT(updateStoredSettingsFromDialog())); 52 | connect(smoothTypeGroup,SIGNAL(buttonReleased(QAbstractButton*)),SLOT(smoothTypeChange(QAbstractButton*))); 53 | // dilateIterationsEdit input string validation 54 | QRegExp rx5("[1-9]\\d{0,1}"); // Integers 1 to 99 55 | QRegExpValidator *validator5 = new QRegExpValidator(rx5, 0); 56 | dilateIterationsEdit->setValidator(validator5); 57 | // erodeIterationsEdit input string validation 58 | QRegExp rx6("[1-9]\\d{0,1}"); // Integers 1 to 99 59 | QRegExpValidator *validator6 = new QRegExpValidator(rx6, 0); 60 | erodeIterationsEdit->setValidator(validator6); 61 | // cannyThresh1Edit input string validation 62 | QRegExp rx7("[0-9]\\d{0,2}"); // Integers 0 to 999 63 | QRegExpValidator *validator7 = new QRegExpValidator(rx7, 0); 64 | cannyThresh1Edit->setValidator(validator7); 65 | // cannyThresh2Edit input string validation 66 | QRegExp rx8("[0-9]\\d{0,2}"); // Integers 0 to 999 67 | QRegExpValidator *validator8 = new QRegExpValidator(rx8, 0); 68 | cannyThresh2Edit->setValidator(validator8); 69 | // cannyApertureSizeEdit input string validation 70 | QRegExp rx9("[3,5,7]\\d{0,0}"); // Integers 3,5,7 71 | QRegExpValidator *validator9 = new QRegExpValidator(rx9, 0); 72 | cannyApertureSizeEdit->setValidator(validator9); 73 | // Set dialog values to defaults 74 | resetAllDialogToDefaults(); 75 | // Update image processing settings in imageProcessingSettings structure and processingThread 76 | updateStoredSettingsFromDialog(); 77 | } // ImageProcessingSettingsDialog constructor 78 | 79 | void ImageProcessingSettingsDialog::updateStoredSettingsFromDialog() 80 | { 81 | // Validate values in dialog before storing 82 | validateDialog(); 83 | // Smooth 84 | if(smoothTypeGroup->checkedButton()==(QAbstractButton*)smoothBlurButton) 85 | imageProcessingSettings.smoothType=0; 86 | else if(smoothTypeGroup->checkedButton()==(QAbstractButton*)smoothGaussianButton) 87 | imageProcessingSettings.smoothType=1; 88 | else if(smoothTypeGroup->checkedButton()==(QAbstractButton*)smoothMedianButton) 89 | imageProcessingSettings.smoothType=2; 90 | imageProcessingSettings.smoothParam1=smoothParam1Edit->text().toInt(); 91 | imageProcessingSettings.smoothParam2=smoothParam2Edit->text().toInt(); 92 | imageProcessingSettings.smoothParam3=smoothParam3Edit->text().toDouble(); 93 | imageProcessingSettings.smoothParam4=smoothParam4Edit->text().toDouble(); 94 | // Dilate 95 | imageProcessingSettings.dilateNumberOfIterations=dilateIterationsEdit->text().toInt(); 96 | // Erode 97 | imageProcessingSettings.erodeNumberOfIterations=erodeIterationsEdit->text().toInt(); 98 | // Flip 99 | if(flipCodeGroup->checkedButton()==(QAbstractButton*)flipXAxisButton) 100 | imageProcessingSettings.flipCode=0; 101 | else if(flipCodeGroup->checkedButton()==(QAbstractButton*)flipYAxisButton) 102 | imageProcessingSettings.flipCode=1; 103 | else if(flipCodeGroup->checkedButton()==(QAbstractButton*)flipBothAxesButton) 104 | imageProcessingSettings.flipCode=-1; 105 | // Canny 106 | imageProcessingSettings.cannyThreshold1=cannyThresh1Edit->text().toDouble(); 107 | imageProcessingSettings.cannyThreshold2=cannyThresh2Edit->text().toDouble(); 108 | imageProcessingSettings.cannyApertureSize=cannyApertureSizeEdit->text().toInt(); 109 | imageProcessingSettings.cannyL2gradient=cannyL2NormCheckBox->isChecked(); 110 | // Update image processing flags in processingThread 111 | emit newImageProcessingSettings(imageProcessingSettings); 112 | } // updateStoredSettingsFromDialog() 113 | 114 | void ImageProcessingSettingsDialog::updateDialogSettingsFromStored() 115 | { 116 | // Smooth 117 | if(imageProcessingSettings.smoothType==0) 118 | smoothBlurButton->setChecked(true); 119 | else if(imageProcessingSettings.smoothType==1) 120 | smoothGaussianButton->setChecked(true); 121 | else if(imageProcessingSettings.smoothType==2) 122 | smoothMedianButton->setChecked(true); 123 | smoothParam1Edit->setText(QString::number(imageProcessingSettings.smoothParam1)); 124 | smoothParam2Edit->setText(QString::number(imageProcessingSettings.smoothParam2)); 125 | smoothParam3Edit->setText(QString::number(imageProcessingSettings.smoothParam3)); 126 | smoothParam4Edit->setText(QString::number(imageProcessingSettings.smoothParam4)); 127 | // Dilate 128 | dilateIterationsEdit->setText(QString::number(imageProcessingSettings.dilateNumberOfIterations)); 129 | // Erode 130 | erodeIterationsEdit->setText(QString::number(imageProcessingSettings.erodeNumberOfIterations)); 131 | // Flip 132 | if(imageProcessingSettings.flipCode==0) 133 | flipXAxisButton->setChecked(true); 134 | else if(imageProcessingSettings.flipCode==1) 135 | flipYAxisButton->setChecked(true); 136 | else if(imageProcessingSettings.flipCode==-1) 137 | flipBothAxesButton->setChecked(true); 138 | // Canny 139 | cannyThresh1Edit->setText(QString::number(imageProcessingSettings.cannyThreshold1)); 140 | cannyThresh2Edit->setText(QString::number(imageProcessingSettings.cannyThreshold2)); 141 | cannyApertureSizeEdit->setText(QString::number(imageProcessingSettings.cannyApertureSize)); 142 | cannyL2NormCheckBox->setChecked(imageProcessingSettings.cannyL2gradient); 143 | // Enable/disable appropriate Smooth parameter inputs 144 | smoothTypeChange(smoothTypeGroup->checkedButton()); 145 | } // updateDialogSettingsFromStored() 146 | 147 | void ImageProcessingSettingsDialog::resetAllDialogToDefaults() 148 | { 149 | // Smooth 150 | resetSmoothDialogToDefaults(); 151 | // Dilate 152 | resetDilateDialogToDefaults(); 153 | // Erode 154 | resetErodeDialogToDefaults(); 155 | // Flip 156 | resetFlipDialogToDefaults(); 157 | // Canny 158 | resetCannyDialogToDefaults(); 159 | } // resetAllDialogToDefaults() 160 | 161 | void ImageProcessingSettingsDialog::smoothTypeChange(QAbstractButton *input) 162 | { 163 | if(input==(QAbstractButton*)smoothBlurButton) 164 | { 165 | // smoothParam1Edit input string validation 166 | QRegExp rx1("[1-9]\\d{0,1}"); // Integers 1 to 99 167 | QRegExpValidator *validator1 = new QRegExpValidator(rx1, 0); 168 | smoothParam1Edit->setValidator(validator1); 169 | // smoothParam2Edit input string validation 170 | QRegExp rx2("[1-9]\\d{0,1}"); // Integers 1 to 99 171 | QRegExpValidator *validator2 = new QRegExpValidator(rx2, 0); 172 | smoothParam2Edit->setValidator(validator2); 173 | // Enable/disable appropriate parameter inputs 174 | smoothParam1Edit->setEnabled(true); 175 | smoothParam2Edit->setEnabled(true); 176 | smoothParam3Edit->setEnabled(false); 177 | smoothParam4Edit->setEnabled(false); 178 | // Set parameter range labels 179 | smoothParam1RangeLabel->setText("[1-99]"); 180 | smoothParam2RangeLabel->setText("[1-99]"); 181 | smoothParam3RangeLabel->setText(""); 182 | smoothParam4RangeLabel->setText(""); 183 | // Set parameter labels 184 | smoothParam1Label->setText("Kernel Width"); 185 | smoothParam2Label->setText("Kernel Height"); 186 | smoothParam3Label->setText(""); 187 | smoothParam4Label->setText(""); 188 | } 189 | else if(input==(QAbstractButton*)smoothGaussianButton) 190 | { 191 | // smoothParam1Edit input string validation 192 | QRegExp rx1("[0-9]\\d{0,1}"); // Integers 0 to 99 193 | QRegExpValidator *validator1 = new QRegExpValidator(rx1, 0); 194 | smoothParam1Edit->setValidator(validator1); 195 | // smoothParam2Edit input string validation 196 | QRegExp rx2("[0-9]\\d{0,1}"); // Integers 0 to 99 197 | QRegExpValidator *validator2 = new QRegExpValidator(rx2, 0); 198 | smoothParam2Edit->setValidator(validator2); 199 | // smoothParam3Edit input string validation 200 | QDoubleValidator *validator3 = new QDoubleValidator(0.0, 99.99, 2, this); 201 | validator3->setNotation(QDoubleValidator::StandardNotation); 202 | smoothParam3Edit->setValidator(validator3); 203 | // Enable/disable appropriate parameter inputs 204 | smoothParam1Edit->setEnabled(true); 205 | smoothParam2Edit->setEnabled(true); 206 | smoothParam3Edit->setEnabled(true); 207 | smoothParam4Edit->setEnabled(true); 208 | // Set parameter range labels 209 | smoothParam1RangeLabel->setText("[0-99]"); 210 | smoothParam2RangeLabel->setText("[0-99]"); 211 | smoothParam3RangeLabel->setText("[0.00-99.99]"); 212 | smoothParam4RangeLabel->setText("[0.00-99.99]"); 213 | // Set parameter labels 214 | smoothParam1Label->setText("Kernel Width"); 215 | smoothParam2Label->setText("Kernel Height"); 216 | smoothParam3Label->setText("Sigma X"); 217 | smoothParam4Label->setText("Sigma Y"); 218 | } 219 | else if(input==(QAbstractButton*)smoothMedianButton) 220 | { 221 | // smoothParam1Edit input string validation 222 | QRegExp rx1("[1-9]\\d{0,1}"); // Integers 1 to 99 223 | QRegExpValidator *validator1 = new QRegExpValidator(rx1, 0); 224 | smoothParam1Edit->setValidator(validator1); 225 | // Enable/disable appropriate parameter inputs 226 | smoothParam1Edit->setEnabled(true); 227 | smoothParam2Edit->setEnabled(false); 228 | smoothParam3Edit->setEnabled(false); 229 | smoothParam4Edit->setEnabled(false); 230 | // Set parameter range labels 231 | smoothParam1RangeLabel->setText("[1-99]"); 232 | smoothParam2RangeLabel->setText(""); 233 | smoothParam3RangeLabel->setText(""); 234 | smoothParam4RangeLabel->setText(""); 235 | // Set parameter labels 236 | smoothParam1Label->setText("Kernel (Square)"); 237 | smoothParam2Label->setText(""); 238 | smoothParam3Label->setText(""); 239 | smoothParam4Label->setText(""); 240 | } 241 | } // smoothTypeChange() 242 | 243 | void ImageProcessingSettingsDialog::validateDialog() 244 | { 245 | // Local variables 246 | bool inputEmpty=false; 247 | 248 | // If value of Smooth parameter 1 is EVEN (and not zero), convert to ODD by adding 1 249 | if(((smoothParam1Edit->text().toInt()%2)==0)&&(smoothParam1Edit->text().toInt()!=0)) 250 | { 251 | smoothParam1Edit->setText(QString::number(smoothParam1Edit->text().toInt()+1)); 252 | QMessageBox::information(this->parentWidget(),"NOTE:","Smooth parameter 1 must be an ODD number.\n\nAutomatically set to (inputted value+1)."); 253 | } 254 | // If value of Smooth parameter 2 is EVEN (and not zero), convert to ODD by adding 1 255 | if(((smoothParam2Edit->text().toInt()%2)==0)&&(smoothParam2Edit->text().toInt()!=0)) 256 | { 257 | smoothParam2Edit->setText(QString::number(smoothParam2Edit->text().toInt()+1)); 258 | QMessageBox::information(this->parentWidget(),"NOTE:","Smooth parameter 2 must be an ODD number (or zero).\n\nAutomatically set to (inputted value+1)."); 259 | } 260 | 261 | // Check for empty inputs: if empty, set to default values 262 | if(smoothParam1Edit->text().isEmpty()) 263 | { 264 | smoothParam1Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_1)); 265 | inputEmpty=true; 266 | } 267 | if(smoothParam2Edit->text().isEmpty()) 268 | { 269 | smoothParam2Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_2)); 270 | inputEmpty=true; 271 | } 272 | if(smoothParam3Edit->text().isEmpty()) 273 | { 274 | smoothParam3Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_3)); 275 | inputEmpty=true; 276 | } 277 | if(smoothParam4Edit->text().isEmpty()) 278 | { 279 | smoothParam4Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_4)); 280 | inputEmpty=true; 281 | } 282 | if(dilateIterationsEdit->text().isEmpty()) 283 | { 284 | dilateIterationsEdit->setText(QString::number(DEFAULT_DILATE_ITERATIONS)); 285 | inputEmpty=true; 286 | } 287 | if(erodeIterationsEdit->text().isEmpty()) 288 | { 289 | erodeIterationsEdit->setText(QString::number(DEFAULT_ERODE_ITERATIONS)); 290 | inputEmpty=true; 291 | } 292 | if(cannyThresh1Edit->text().isEmpty()) 293 | { 294 | cannyThresh1Edit->setText(QString::number(DEFAULT_CANNY_THRESHOLD_1)); 295 | inputEmpty=true; 296 | } 297 | if(cannyThresh2Edit->text().isEmpty()) 298 | { 299 | cannyThresh2Edit->setText(QString::number(DEFAULT_CANNY_THRESHOLD_2)); 300 | inputEmpty=true; 301 | } 302 | if(cannyApertureSizeEdit->text().isEmpty()) 303 | { 304 | cannyApertureSizeEdit->setText(QString::number(DEFAULT_CANNY_APERTURE_SIZE)); 305 | inputEmpty=true; 306 | } 307 | // Check if any of the inputs were empty 308 | if(inputEmpty) 309 | QMessageBox::warning(this->parentWidget(),"WARNING:","One or more inputs empty.\n\nAutomatically set to default values."); 310 | 311 | // Check for special parameter cases when smoothing type is GAUSSIAN 312 | if((smoothTypeGroup->checkedButton()==(QAbstractButton*)smoothGaussianButton)&& 313 | (smoothParam1Edit->text().toInt()==0)&&(smoothParam3Edit->text().toDouble()==0.00)) 314 | { 315 | smoothParam1Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_1)); 316 | smoothParam3Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_3)); 317 | QMessageBox::warning(this->parentWidget(),"ERROR:","Parameters 1 and 3 cannot BOTH be zero when the smoothing type is GAUSSIAN.\n\nAutomatically set to default values."); 318 | } 319 | if((smoothTypeGroup->checkedButton()==(QAbstractButton*)smoothGaussianButton)&& 320 | (smoothParam2Edit->text().toInt()==0)&&(smoothParam4Edit->text().toDouble()==0.00)) 321 | { 322 | smoothParam2Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_2)); 323 | smoothParam4Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_4)); 324 | QMessageBox::warning(this->parentWidget(),"ERROR:","Parameters 2 and 4 cannot BOTH be zero when the smoothing type is GAUSSIAN.\n\nAutomatically set to default values."); 325 | } 326 | // Ensure neither smoothing parameters 1 or 2 are ZERO (except in the GAUSSIAN case) 327 | if((smoothTypeGroup->checkedButton()!=(QAbstractButton*)smoothGaussianButton)&& 328 | ((smoothParam1Edit->text().toInt()==0)||(smoothParam2Edit->text().toDouble()==0))) 329 | { 330 | smoothParam1Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_1)); 331 | smoothParam2Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_2)); 332 | QMessageBox::warning(this->parentWidget(),"ERROR:","Parameters 1 or 2 cannot be zero for the current smoothing type.\n\nAutomatically set to default values."); 333 | } 334 | } // validateDialog() 335 | 336 | void ImageProcessingSettingsDialog::resetSmoothDialogToDefaults() 337 | { 338 | if(DEFAULT_SMOOTH_TYPE==0) 339 | smoothBlurButton->setChecked(true); 340 | else if(DEFAULT_SMOOTH_TYPE==1) 341 | smoothGaussianButton->setChecked(true); 342 | else if(DEFAULT_SMOOTH_TYPE==2) 343 | smoothMedianButton->setChecked(true); 344 | smoothParam1Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_1)); 345 | smoothParam2Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_2)); 346 | smoothParam3Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_3)); 347 | smoothParam4Edit->setText(QString::number(DEFAULT_SMOOTH_PARAM_4)); 348 | // Enable/disable appropriate Smooth parameter inputs 349 | smoothTypeChange(smoothTypeGroup->checkedButton()); 350 | } // resetSmoothDialogToDefaults() 351 | 352 | void ImageProcessingSettingsDialog::resetDilateDialogToDefaults() 353 | { 354 | dilateIterationsEdit->setText(QString::number(DEFAULT_DILATE_ITERATIONS)); 355 | } // resetDilateDialogToDefaults() 356 | 357 | void ImageProcessingSettingsDialog::resetErodeDialogToDefaults() 358 | { 359 | erodeIterationsEdit->setText(QString::number(DEFAULT_ERODE_ITERATIONS)); 360 | } // resetErodeDialogToDefaults() 361 | 362 | void ImageProcessingSettingsDialog::resetFlipDialogToDefaults() 363 | { 364 | if(DEFAULT_FLIP_CODE==0) 365 | flipXAxisButton->setChecked(true); 366 | else if(DEFAULT_FLIP_CODE==1) 367 | flipYAxisButton->setChecked(true); 368 | else if(DEFAULT_FLIP_CODE==-1) 369 | flipBothAxesButton->setChecked(true); 370 | } // resetFlipDialogToDefaults() 371 | 372 | void ImageProcessingSettingsDialog::resetCannyDialogToDefaults() 373 | { 374 | cannyThresh1Edit->setText(QString::number(DEFAULT_CANNY_THRESHOLD_1)); 375 | cannyThresh2Edit->setText(QString::number(DEFAULT_CANNY_THRESHOLD_2)); 376 | cannyApertureSizeEdit->setText(QString::number(DEFAULT_CANNY_APERTURE_SIZE)); 377 | cannyL2NormCheckBox->setChecked(DEFAULT_CANNY_L2GRADIENT); 378 | } // resetCannyDialogToDefaults() 379 | -------------------------------------------------------------------------------- /ImageProcessingSettingsDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* ImageProcessingSettingsDialog.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef IMAGEPROCESSINGSETTINGSDIALOG_H 34 | #define IMAGEPROCESSINGSETTINGSDIALOG_H 35 | 36 | #include "ui_ImageProcessingSettingsDialog.h" 37 | #include "Structures.h" 38 | 39 | class ImageProcessingSettingsDialog : public QDialog, private Ui::ImageProcessingSettingsDialog 40 | { 41 | Q_OBJECT 42 | 43 | public: 44 | ImageProcessingSettingsDialog(QWidget *parent = 0); 45 | void updateDialogSettingsFromStored(); 46 | private: 47 | ImageProcessingSettings imageProcessingSettings; 48 | public slots: 49 | void resetAllDialogToDefaults(); 50 | void updateStoredSettingsFromDialog(); 51 | private slots: 52 | void resetSmoothDialogToDefaults(); 53 | void resetDilateDialogToDefaults(); 54 | void resetErodeDialogToDefaults(); 55 | void resetFlipDialogToDefaults(); 56 | void resetCannyDialogToDefaults(); 57 | void validateDialog(); 58 | void smoothTypeChange(QAbstractButton *); 59 | signals: 60 | void newImageProcessingSettings(struct ImageProcessingSettings p_settings); 61 | }; 62 | 63 | #endif // IMAGEPROCESSINGSETTINGSDIALOG_H 64 | -------------------------------------------------------------------------------- /ImageProcessingSettingsDialog.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | ImageProcessingSettingsDialog 4 | 5 | 6 | 7 | 0 8 | 0 9 | 441 10 | 381 11 | 12 | 13 | 14 | 15 | 0 16 | 0 17 | 18 | 19 | 20 | 21 | 0 22 | 0 23 | 24 | 25 | 26 | Image Processing Settings 27 | 28 | 29 | 30 | 31 | 11 32 | 11 33 | 421 34 | 361 35 | 36 | 37 | 38 | 39 | 40 | 41 | 0 42 | 43 | 44 | 45 | Smooth 46 | 47 | 48 | 49 | 50 | 10 51 | 10 52 | 401 53 | 221 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 0 66 | 0 67 | 68 | 69 | 70 | 71 | 0 72 | 27 73 | 74 | 75 | 76 | 77 | 140 78 | 27 79 | 80 | 81 | 82 | 83 | 8 84 | 75 85 | true 86 | 87 | 88 | 89 | Type: 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 140 100 | 16777215 101 | 102 | 103 | 104 | 105 | 8 106 | 107 | 108 | 109 | Blur 110 | 111 | 112 | smoothTypeGroup 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 140 121 | 16777215 122 | 123 | 124 | 125 | 126 | 8 127 | 128 | 129 | 130 | Gaussian 131 | 132 | 133 | smoothTypeGroup 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 140 142 | 16777215 143 | 144 | 145 | 146 | 147 | 8 148 | 149 | 150 | 151 | Median 152 | 153 | 154 | smoothTypeGroup 155 | 156 | 157 | 158 | 159 | 160 | 161 | Qt::Vertical 162 | 163 | 164 | 165 | 20 166 | 40 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 0 182 | 27 183 | 184 | 185 | 186 | 187 | 16777215 188 | 27 189 | 190 | 191 | 192 | 193 | 8 194 | 75 195 | true 196 | 197 | 198 | 199 | Parameters: 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 0 212 | 0 213 | 214 | 215 | 216 | 217 | 15 218 | 0 219 | 220 | 221 | 222 | 223 | 15 224 | 16777215 225 | 226 | 227 | 228 | 229 | 8 230 | 231 | 232 | 233 | 1: 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 0 242 | 0 243 | 244 | 245 | 246 | 247 | 45 248 | 0 249 | 250 | 251 | 252 | 253 | 45 254 | 16777215 255 | 256 | 257 | 258 | 259 | 8 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 8 269 | 75 270 | true 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 8 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 0 299 | 0 300 | 301 | 302 | 303 | 304 | 15 305 | 0 306 | 307 | 308 | 309 | 310 | 15 311 | 16777215 312 | 313 | 314 | 315 | 316 | 8 317 | 318 | 319 | 320 | 2: 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 0 329 | 0 330 | 331 | 332 | 333 | 334 | 45 335 | 0 336 | 337 | 338 | 339 | 340 | 45 341 | 16777215 342 | 343 | 344 | 345 | 346 | 8 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 8 356 | 75 357 | true 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 8 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 0 386 | 0 387 | 388 | 389 | 390 | 391 | 15 392 | 0 393 | 394 | 395 | 396 | 397 | 15 398 | 16777215 399 | 400 | 401 | 402 | 403 | 8 404 | 405 | 406 | 407 | 3: 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 0 416 | 0 417 | 418 | 419 | 420 | 421 | 45 422 | 0 423 | 424 | 425 | 426 | 427 | 45 428 | 16777215 429 | 430 | 431 | 432 | 433 | 8 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 8 443 | 75 444 | true 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 8 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 0 473 | 0 474 | 475 | 476 | 477 | 478 | 15 479 | 0 480 | 481 | 482 | 483 | 484 | 15 485 | 16777215 486 | 487 | 488 | 489 | 490 | 8 491 | 492 | 493 | 494 | 4: 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 0 503 | 0 504 | 505 | 506 | 507 | 508 | 45 509 | 0 510 | 511 | 512 | 513 | 514 | 45 515 | 16777215 516 | 517 | 518 | 519 | 520 | 8 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 8 530 | 75 531 | true 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 8 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | Qt::Vertical 557 | 558 | 559 | 560 | 20 561 | 40 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | Qt::Vertical 576 | 577 | 578 | 579 | 20 580 | 40 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | Reset to Defaults 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | Dilate 598 | 599 | 600 | 601 | 602 | 10 603 | 10 604 | 401 605 | 221 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 0 616 | 27 617 | 618 | 619 | 620 | 621 | 8 622 | 75 623 | true 624 | 625 | 626 | 627 | Number of iterations: 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 0 636 | 0 637 | 638 | 639 | 640 | 641 | 50 642 | 27 643 | 644 | 645 | 646 | 647 | 50 648 | 16777215 649 | 650 | 651 | 652 | 653 | 8 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 0 663 | 27 664 | 665 | 666 | 667 | 668 | 8 669 | 75 670 | true 671 | 672 | 673 | 674 | [1-99] 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | Qt::Vertical 684 | 685 | 686 | 687 | 20 688 | 40 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | Reset to Defaults 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | Erode 706 | 707 | 708 | 709 | 710 | 10 711 | 10 712 | 401 713 | 221 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 0 724 | 27 725 | 726 | 727 | 728 | 729 | 8 730 | 75 731 | true 732 | 733 | 734 | 735 | Number of iterations: 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 0 744 | 0 745 | 746 | 747 | 748 | 749 | 50 750 | 27 751 | 752 | 753 | 754 | 755 | 50 756 | 16777215 757 | 758 | 759 | 760 | 761 | 8 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 0 771 | 27 772 | 773 | 774 | 775 | 776 | 8 777 | 75 778 | true 779 | 780 | 781 | 782 | [1-99] 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | Qt::Vertical 792 | 793 | 794 | 795 | 20 796 | 40 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | Reset to Defaults 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | Flip 814 | 815 | 816 | 817 | 818 | 10 819 | 10 820 | 401 821 | 221 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 0 832 | 0 833 | 834 | 835 | 836 | 837 | 0 838 | 27 839 | 840 | 841 | 842 | 843 | 16777215 844 | 27 845 | 846 | 847 | 848 | 849 | 8 850 | 75 851 | true 852 | 853 | 854 | 855 | Mode: 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 8 864 | 865 | 866 | 867 | X-axis 868 | 869 | 870 | flipCodeGroup 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 8 879 | 880 | 881 | 882 | Y-axis 883 | 884 | 885 | flipCodeGroup 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 8 894 | 895 | 896 | 897 | Both axes 898 | 899 | 900 | flipCodeGroup 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | Qt::Vertical 910 | 911 | 912 | 913 | 20 914 | 40 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | Reset to Defaults 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | Canny 932 | 933 | 934 | 935 | 936 | 10 937 | 10 938 | 401 939 | 221 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 0 950 | 27 951 | 952 | 953 | 954 | 955 | 8 956 | 75 957 | true 958 | 959 | 960 | 961 | Threshold 1: 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 0 970 | 0 971 | 972 | 973 | 974 | 975 | 50 976 | 27 977 | 978 | 979 | 980 | 981 | 50 982 | 16777215 983 | 984 | 985 | 986 | 987 | 8 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 0 997 | 27 998 | 999 | 1000 | 1001 | 1002 | 8 1003 | 75 1004 | true 1005 | 1006 | 1007 | 1008 | [0-999] 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 0 1021 | 27 1022 | 1023 | 1024 | 1025 | 1026 | 8 1027 | 75 1028 | true 1029 | 1030 | 1031 | 1032 | Threshold 2: 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 0 1041 | 0 1042 | 1043 | 1044 | 1045 | 1046 | 50 1047 | 27 1048 | 1049 | 1050 | 1051 | 1052 | 50 1053 | 16777215 1054 | 1055 | 1056 | 1057 | 1058 | 8 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 0 1068 | 27 1069 | 1070 | 1071 | 1072 | 1073 | 8 1074 | 75 1075 | true 1076 | 1077 | 1078 | 1079 | [0-999] 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 0 1092 | 27 1093 | 1094 | 1095 | 1096 | 1097 | 8 1098 | 75 1099 | true 1100 | 1101 | 1102 | 1103 | Aperture Size: 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | 0 1112 | 0 1113 | 1114 | 1115 | 1116 | 1117 | 50 1118 | 27 1119 | 1120 | 1121 | 1122 | 1123 | 50 1124 | 16777215 1125 | 1126 | 1127 | 1128 | 1129 | 8 1130 | 1131 | 1132 | 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 0 1139 | 27 1140 | 1141 | 1142 | 1143 | 1144 | 8 1145 | 75 1146 | true 1147 | 1148 | 1149 | 1150 | [3/5/7] 1151 | 1152 | 1153 | 1154 | 1155 | 1156 | 1157 | 1158 | 1159 | 1160 | 0 1161 | 27 1162 | 1163 | 1164 | 1165 | 1166 | 8 1167 | 75 1168 | true 1169 | 1170 | 1171 | 1172 | Use L2-norm 1173 | 1174 | 1175 | 1176 | 1177 | 1178 | 1179 | Qt::Vertical 1180 | 1181 | 1182 | 1183 | 20 1184 | 40 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191 | 1192 | Reset to Defaults 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | Qt::Horizontal 1205 | 1206 | 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | Apply 1214 | 1215 | 1216 | 1217 | 1218 | 1219 | 1220 | Reset All to Defaults 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | Qt::Horizontal 1230 | 1231 | 1232 | 1233 | 1234 | 1235 | 1236 | Qt::Horizontal 1237 | 1238 | 1239 | QDialogButtonBox::Cancel|QDialogButtonBox::Ok 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | tabWidget 1248 | smoothBlurButton 1249 | smoothGaussianButton 1250 | smoothMedianButton 1251 | smoothParam1Edit 1252 | smoothParam2Edit 1253 | smoothParam3Edit 1254 | smoothParam4Edit 1255 | resetSmoothToDefaultsButton 1256 | dilateIterationsEdit 1257 | resetDilateToDefaultsButton 1258 | erodeIterationsEdit 1259 | resetErodeToDefaultsButton 1260 | flipXAxisButton 1261 | flipYAxisButton 1262 | flipBothAxesButton 1263 | resetFlipToDefaultsButton 1264 | cannyThresh1Edit 1265 | cannyThresh2Edit 1266 | cannyApertureSizeEdit 1267 | cannyL2NormCheckBox 1268 | resetCannyToDefaultsButton 1269 | applyButton 1270 | resetAllToDefaultsButton 1271 | okCancelBox 1272 | 1273 | 1274 | 1275 | 1276 | okCancelBox 1277 | accepted() 1278 | ImageProcessingSettingsDialog 1279 | accept() 1280 | 1281 | 1282 | 430 1283 | 136 1284 | 1285 | 1286 | 157 1287 | 274 1288 | 1289 | 1290 | 1291 | 1292 | okCancelBox 1293 | rejected() 1294 | ImageProcessingSettingsDialog 1295 | reject() 1296 | 1297 | 1298 | 430 1299 | 136 1300 | 1301 | 1302 | 286 1303 | 274 1304 | 1305 | 1306 | 1307 | 1308 | 1309 | 1310 | 1311 | 1312 | 1313 | -------------------------------------------------------------------------------- /MainWindow.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* MainWindow.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "CameraConnectDialog.h" 34 | #include "ImageProcessingSettingsDialog.h" 35 | #include "Controller.h" 36 | #include "MainWindow.h" 37 | 38 | // Qt header files 39 | #include 40 | // Configuration header file 41 | #include "Config.h" 42 | 43 | MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) 44 | { 45 | // Setup user interface 46 | setupUi(this); 47 | // Create controller 48 | controller = new Controller; 49 | // Save application version in QString variable 50 | appVersion=QUOTE(APP_VERSION); 51 | // Initialize data structures 52 | initializeImageProcessingFlagsStructure(); 53 | initializeTaskDataStructure(); 54 | // Initialize GUI 55 | setInitialGUIState(); 56 | // Connect signals to slots 57 | signalSlotsInit(); 58 | // Initialize flag 59 | isCameraConnected=false; 60 | } // MainWindow constructor 61 | 62 | MainWindow::~MainWindow() 63 | { 64 | // Disconnect camera if connected 65 | if(isCameraConnected) 66 | controller->disconnectCamera(); 67 | } // MainWindow destructor 68 | 69 | void MainWindow::connectToCamera() 70 | { 71 | // Create CameraConnectDialog instance 72 | cameraConnectDialog = new CameraConnectDialog(this); 73 | // Create ProcessingSettingsDialog instance 74 | imageProcessingSettingsDialog = new ImageProcessingSettingsDialog(this); 75 | // PROMPT USER: 76 | // If user presses OK button on dialog, connect to camera; else do nothing 77 | if(cameraConnectDialog->exec()==1) 78 | { 79 | // Store image buffer size in local variable 80 | imageBufferSize=cameraConnectDialog->getImageBufferSize(); 81 | // Store device number in local variable 82 | deviceNumber=cameraConnectDialog->getDeviceNumber(); 83 | // Connect to camera 84 | if((isCameraConnected=controller->connectToCamera(deviceNumber,imageBufferSize, 85 | cameraConnectDialog->getDropFrameCheckBoxState(), 86 | cameraConnectDialog->getCaptureThreadPrio(), 87 | cameraConnectDialog->getProcessingThreadPrio()))) 88 | { 89 | // Create connection between frameLabel (emitter) and GUI thread (receiver/listener) 90 | connect(this->frameLabel,SIGNAL(newMouseData(struct MouseData)),this,SLOT(newMouseData(struct MouseData))); 91 | // Create connection between processing thread (emitter) and GUI thread (receiver/listener) 92 | connect(controller->processingThread,SIGNAL(newFrame(QImage)),this,SLOT(updateFrame(QImage))); 93 | // Create connections (3) between GUI thread (emitter) and processing thread (receiver/listener) 94 | connect(this->imageProcessingSettingsDialog,SIGNAL(newImageProcessingSettings(struct ImageProcessingSettings)),controller->processingThread,SLOT(updateImageProcessingSettings(struct ImageProcessingSettings))); 95 | connect(this,SIGNAL(newImageProcessingFlags(struct ImageProcessingFlags)),controller->processingThread,SLOT(updateImageProcessingFlags(struct ImageProcessingFlags))); 96 | connect(this,SIGNAL(newTaskData(struct TaskData)),controller->processingThread,SLOT(updateTaskData(struct TaskData))); 97 | // Initialize data structures 98 | initializeImageProcessingFlagsStructure(); 99 | initializeTaskDataStructure(); 100 | // Set data to defaults in processingThread 101 | emit newImageProcessingFlags(imageProcessingFlags); 102 | emit newTaskData(taskData); 103 | imageProcessingSettingsDialog->updateStoredSettingsFromDialog(); 104 | // Setup imageBufferBar in main window with minimum and maximum values 105 | imageBufferBar->setMinimum(0); 106 | imageBufferBar->setMaximum(imageBufferSize); 107 | // Enable/disable appropriate GUI items 108 | connectToCameraAction->setEnabled(false); 109 | disconnectCameraAction->setEnabled(true); 110 | imageProcessingSettingsAction->setEnabled(true); 111 | imageProcessingMenu->setEnabled(true); 112 | // Enable "Clear Image Buffer" push button in main window 113 | clearImageBufferButton->setEnabled(true); 114 | // Get input stream properties 115 | sourceWidth=controller->captureThread->getInputSourceWidth(); 116 | sourceHeight=controller->captureThread->getInputSourceHeight(); 117 | // Set text in labels in main window 118 | deviceNumberLabel->setNum(deviceNumber); 119 | cameraResolutionLabel->setText(QString::number(sourceWidth)+QString("x")+QString::number(sourceHeight)); 120 | } 121 | // Display error dialog if camera connection is unsuccessful 122 | else 123 | { 124 | QMessageBox::warning(this,"ERROR:","Could not connect to camera."); 125 | // Delete dialogs 126 | delete cameraConnectDialog; 127 | delete imageProcessingSettingsDialog; 128 | } 129 | } 130 | } // connectToCamera() 131 | 132 | void MainWindow::disconnectCamera() 133 | { 134 | // Check if camera is connected 135 | if(controller->captureThread->isCameraConnected()) 136 | { 137 | // Disconnect connections (5) 138 | disconnect(this->frameLabel,SIGNAL(newMouseData(struct MouseData)),this,SLOT(newMouseData(struct MouseData))); 139 | disconnect(controller->processingThread,SIGNAL(newFrame(QImage)),0,0); 140 | disconnect(this->imageProcessingSettingsDialog,SIGNAL(newImageProcessingSettings(struct ImageProcessingSettings)),controller->processingThread,SLOT(updateImageProcessingSettings(struct ImageProcessingSettings))); 141 | disconnect(this,SIGNAL(newImageProcessingFlags(struct ImageProcessingFlags)),controller->processingThread,SLOT(updateImageProcessingFlags(struct ImageProcessingFlags))); 142 | disconnect(this,SIGNAL(newTaskData(struct TaskData)),controller->processingThread,SLOT(updateTaskData(struct TaskData))); 143 | // Delete dialogs 144 | delete cameraConnectDialog; 145 | delete imageProcessingSettingsDialog; 146 | // Reset flag 147 | isCameraConnected=false; 148 | // Set GUI 149 | setInitialGUIState(); 150 | // Disconnect camera 151 | controller->disconnectCamera(); 152 | } 153 | // Display error dialog 154 | else 155 | QMessageBox::warning(this,"ERROR:","Camera already disconnected."); 156 | } // disconnectCamera() 157 | 158 | void MainWindow::about() 159 | { 160 | QMessageBox::information(this,"About",QString("Created by Nick D'Ademo\n\nContact: nickdademo@gmail.com\nWebsite: www.nickdademo.com\n\nVersion: ")+appVersion); 161 | } // about() 162 | 163 | void MainWindow::clearImageBuffer() 164 | { 165 | controller->clearImageBuffer(); 166 | } // clearImageBuffer() 167 | 168 | void MainWindow::setGrayscale(bool input) 169 | { 170 | // Not checked 171 | if(!input) 172 | imageProcessingFlags.grayscaleOn=false; 173 | // Checked 174 | else if(input) 175 | imageProcessingFlags.grayscaleOn=true; 176 | // Update image processing flags in processingThread 177 | emit newImageProcessingFlags(imageProcessingFlags); 178 | } // setGrayscale() 179 | 180 | void MainWindow::setSmooth(bool input) 181 | { 182 | // Not checked 183 | if(!input) 184 | imageProcessingFlags.smoothOn=false; 185 | // Checked 186 | else if(input) 187 | imageProcessingFlags.smoothOn=true; 188 | // Update image processing flags in processingThread 189 | emit newImageProcessingFlags(imageProcessingFlags); 190 | } // setSmooth() 191 | 192 | void MainWindow::setDilate(bool input) 193 | { 194 | // Not checked 195 | if(!input) 196 | imageProcessingFlags.dilateOn=false; 197 | // Checked 198 | else if(input) 199 | imageProcessingFlags.dilateOn=true; 200 | // Update image processing flags in processingThread 201 | emit newImageProcessingFlags(imageProcessingFlags); 202 | } // setDilate() 203 | 204 | void MainWindow::setErode(bool input) 205 | { 206 | // Not checked 207 | if(!input) 208 | imageProcessingFlags.erodeOn=false; 209 | // Checked 210 | else if(input) 211 | imageProcessingFlags.erodeOn=true; 212 | // Update image processing flags in processingThread 213 | emit newImageProcessingFlags(imageProcessingFlags); 214 | } // setErode() 215 | 216 | void MainWindow::setFlip(bool input) 217 | { 218 | // Not checked 219 | if(!input) 220 | imageProcessingFlags.flipOn=false; 221 | // Checked 222 | else if(input) 223 | imageProcessingFlags.flipOn=true; 224 | // Update image processing flags in processingThread 225 | emit newImageProcessingFlags(imageProcessingFlags); 226 | } // setFlip() 227 | 228 | void MainWindow::setCanny(bool input) 229 | { 230 | // Not checked 231 | if(!input) 232 | imageProcessingFlags.cannyOn=false; 233 | // Checked 234 | else if(input) 235 | imageProcessingFlags.cannyOn=true; 236 | // Update image processing flags in processingThread 237 | emit newImageProcessingFlags(imageProcessingFlags); 238 | } // setCanny() 239 | 240 | void MainWindow::updateFrame(const QImage &frame) 241 | { 242 | // Show [number of images in buffer / image buffer size] in imageBufferLabel in main window 243 | imageBufferLabel->setText(QString("[")+QString::number(controller->processingThread->getCurrentSizeOfBuffer())+ 244 | QString("/")+QString::number(imageBufferSize)+QString("]")); 245 | // Show percentage of image bufffer full in imageBufferBar in main window 246 | imageBufferBar->setValue(controller->processingThread->getCurrentSizeOfBuffer()); 247 | // Show processing rate in captureRateLabel in main window 248 | captureRateLabel->setNum(controller->captureThread->getAvgFPS()); 249 | captureRateLabel->setText(captureRateLabel->text()+" fps"); 250 | // Show processing rate in processingRateLabel in main window 251 | processingRateLabel->setNum(controller->processingThread->getAvgFPS()); 252 | processingRateLabel->setText(processingRateLabel->text()+" fps"); 253 | // Show ROI information in roiLabel in main window 254 | roiLabel->setText(QString("(")+QString::number(controller->processingThread->getCurrentROI().x)+QString(",")+ 255 | QString::number(controller->processingThread->getCurrentROI().y)+QString(") ")+ 256 | QString::number(controller->processingThread->getCurrentROI().width)+ 257 | QString("x")+QString::number(controller->processingThread->getCurrentROI().height)); 258 | // Display frame in main window 259 | frameLabel->setPixmap(QPixmap::fromImage(frame)); 260 | } // updateFrame() 261 | 262 | void MainWindow::setImageProcessingSettings() 263 | { 264 | // Prompt user: 265 | // If user presses OK button on dialog, update image processing settings 266 | if(imageProcessingSettingsDialog->exec()==1) 267 | imageProcessingSettingsDialog->updateStoredSettingsFromDialog(); 268 | // Else, restore dialog state 269 | else 270 | imageProcessingSettingsDialog->updateDialogSettingsFromStored(); 271 | } // setImageProcessingSettings() 272 | 273 | void MainWindow::updateMouseCursorPosLabel() 274 | { 275 | // Update mouse cursor position in mouseCursorPosLabel in main window 276 | mouseCursorPosLabel->setText(QString("(")+QString::number(frameLabel->getMouseCursorPos().x())+ 277 | QString(",")+QString::number(frameLabel->getMouseCursorPos().y())+ 278 | QString(")")); 279 | // Show ROI-adjusted cursor position if camera is connected 280 | if(isCameraConnected) 281 | mouseCursorPosLabel->setText(mouseCursorPosLabel->text()+ 282 | QString(" [")+QString::number(frameLabel->getMouseCursorPos().x()-(640-controller->processingThread->getCurrentROI().width)/2)+ 283 | QString(",")+QString::number(frameLabel->getMouseCursorPos().y()-(480-controller->processingThread->getCurrentROI().height)/2)+ 284 | QString("]")); 285 | } // updateMouseCursorPosLabel() 286 | 287 | void MainWindow::newMouseData(struct MouseData mouseData) 288 | { 289 | // Local variables 290 | int x_temp, y_temp, width_temp, height_temp; 291 | // Set ROI 292 | if(mouseData.leftButtonRelease) 293 | { 294 | // Copy box dimensions from mouseData to taskData 295 | taskData.selectionBox.setX(mouseData.selectionBox.x()-((frameLabel->width()-controller->captureThread->getInputSourceWidth()))/2); 296 | taskData.selectionBox.setY(mouseData.selectionBox.y()-((frameLabel->height()-controller->captureThread->getInputSourceHeight()))/2); 297 | taskData.selectionBox.setWidth(mouseData.selectionBox.width()); 298 | taskData.selectionBox.setHeight(mouseData.selectionBox.height()); 299 | // Check if selection box has NON-ZERO dimensions 300 | if((taskData.selectionBox.width()!=0)&&((taskData.selectionBox.height())!=0)) 301 | { 302 | // Selection box can also be drawn from bottom-right to top-left corner 303 | if(taskData.selectionBox.width()<0) 304 | { 305 | x_temp=taskData.selectionBox.x(); 306 | width_temp=taskData.selectionBox.width(); 307 | taskData.selectionBox.setX(x_temp+taskData.selectionBox.width()); 308 | taskData.selectionBox.setWidth(width_temp*-1); 309 | } 310 | if(taskData.selectionBox.height()<0) 311 | { 312 | y_temp=taskData.selectionBox.y(); 313 | height_temp=taskData.selectionBox.height(); 314 | taskData.selectionBox.setY(y_temp+taskData.selectionBox.height()); 315 | taskData.selectionBox.setHeight(height_temp*-1); 316 | } 317 | // Check if selection box is not outside window 318 | if((taskData.selectionBox.x()<0)||(taskData.selectionBox.y()<0)|| 319 | ((taskData.selectionBox.x()+taskData.selectionBox.width())>sourceWidth)|| 320 | ((taskData.selectionBox.y()+taskData.selectionBox.height())>sourceHeight)) 321 | { 322 | // Display error message 323 | QMessageBox::warning(this,"ERROR:","Selection box outside range. Please try again."); 324 | } 325 | else 326 | { 327 | // Set setROIFlag to TRUE 328 | taskData.setROIFlag=true; 329 | // Update task data in processingThread 330 | emit newTaskData(taskData); 331 | // Set setROIFlag to FALSE 332 | taskData.setROIFlag=false; 333 | } 334 | } 335 | } 336 | // Reset ROI 337 | else if(mouseData.rightButtonRelease) 338 | { 339 | // Set resetROIFlag to TRUE 340 | taskData.resetROIFlag=true; 341 | // Update task data in processingThread 342 | emit newTaskData(taskData); 343 | // Set resetROIFlag to FALSE 344 | taskData.resetROIFlag=false; 345 | } 346 | } // newMouseData() 347 | 348 | void MainWindow::initializeImageProcessingFlagsStructure() 349 | { 350 | imageProcessingFlags.grayscaleOn=false; 351 | imageProcessingFlags.smoothOn=false; 352 | imageProcessingFlags.dilateOn=false; 353 | imageProcessingFlags.erodeOn=false; 354 | imageProcessingFlags.flipOn=false; 355 | imageProcessingFlags.cannyOn=false; 356 | } // initializeImageProcessingFlagsStructure() 357 | 358 | void MainWindow::initializeTaskDataStructure() 359 | { 360 | taskData.setROIFlag=false; 361 | taskData.resetROIFlag=false; 362 | } // initializeTaskDataStructure() 363 | 364 | void MainWindow::setInitialGUIState() 365 | { 366 | ////////////////////////////////////////// 367 | // Enable/disable appropriate GUI items // 368 | ////////////////////////////////////////// 369 | // Menu 370 | connectToCameraAction->setDisabled(false); 371 | disconnectCameraAction->setDisabled(true); 372 | imageProcessingMenu->setDisabled(true); 373 | grayscaleAction->setChecked(false); 374 | smoothAction->setChecked(false); 375 | dilateAction->setChecked(false); 376 | erodeAction->setChecked(false); 377 | flipAction->setChecked(false); 378 | cannyAction->setChecked(false); 379 | // Window 380 | frameLabel->setText("No camera connected."); 381 | imageBufferBar->setValue(0); 382 | imageBufferLabel->setText("[000/000]"); 383 | captureRateLabel->setText(""); 384 | processingRateLabel->setText(""); 385 | deviceNumberLabel->setText(""); 386 | cameraResolutionLabel->setText(""); 387 | roiLabel->setText(""); 388 | mouseCursorPosLabel->setText(""); 389 | clearImageBufferButton->setDisabled(true); 390 | } // setInitialGUIState() 391 | 392 | void MainWindow::signalSlotsInit() 393 | { 394 | connect(connectToCameraAction, SIGNAL(triggered()), this, SLOT(connectToCamera())); 395 | connect(disconnectCameraAction, SIGNAL(triggered()), this, SLOT(disconnectCamera())); 396 | connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); 397 | connect(grayscaleAction, SIGNAL(toggled(bool)), this, SLOT(setGrayscale(bool))); 398 | connect(smoothAction, SIGNAL(toggled(bool)), this, SLOT(setSmooth(bool))); 399 | connect(dilateAction, SIGNAL(toggled(bool)), this, SLOT(setDilate(bool))); 400 | connect(erodeAction, SIGNAL(toggled(bool)), this, SLOT(setErode(bool))); 401 | connect(flipAction, SIGNAL(toggled(bool)), this, SLOT(setFlip(bool))); 402 | connect(cannyAction, SIGNAL(toggled(bool)), this, SLOT(setCanny(bool))); 403 | connect(imageProcessingSettingsAction, SIGNAL(triggered()), this, SLOT(setImageProcessingSettings())); 404 | connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); 405 | connect(clearImageBufferButton, SIGNAL(released()), this, SLOT(clearImageBuffer())); 406 | connect(frameLabel, SIGNAL(onMouseMoveEvent()), this, SLOT(updateMouseCursorPosLabel())); 407 | } // signalSlotsInit() 408 | -------------------------------------------------------------------------------- /MainWindow.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* MainWindow.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef MAINWINDOW_H 34 | #define MAINWINDOW_H 35 | 36 | #include "ui_MainWindow.h" 37 | #include "Structures.h" 38 | 39 | #define QUOTE_(x) #x 40 | #define QUOTE(x) QUOTE_(x) 41 | 42 | class CameraConnectDialog; 43 | class ImageProcessingSettingsDialog; 44 | class Controller; 45 | 46 | class MainWindow : public QMainWindow, private Ui::MainWindow 47 | { 48 | Q_OBJECT 49 | 50 | public: 51 | MainWindow(QWidget *parent = 0); 52 | ~MainWindow(); 53 | void initializeImageProcessingFlagsStructure(); 54 | void initializeTaskDataStructure(); 55 | void setInitialGUIState(); 56 | void signalSlotsInit(); 57 | private: 58 | CameraConnectDialog *cameraConnectDialog; 59 | ImageProcessingSettingsDialog *imageProcessingSettingsDialog; 60 | Controller *controller; 61 | ImageProcessingFlags imageProcessingFlags; 62 | TaskData taskData; 63 | QString appVersion; 64 | int sourceWidth; 65 | int sourceHeight; 66 | int deviceNumber; 67 | int imageBufferSize; 68 | bool isCameraConnected; 69 | public slots: 70 | void connectToCamera(); 71 | void disconnectCamera(); 72 | void about(); 73 | void clearImageBuffer(); 74 | void setGrayscale(bool); 75 | void setSmooth(bool); 76 | void setDilate(bool); 77 | void setErode(bool); 78 | void setFlip(bool); 79 | void setCanny(bool); 80 | void setImageProcessingSettings(); 81 | void updateMouseCursorPosLabel(); 82 | void newMouseData(struct MouseData); 83 | private slots: 84 | void updateFrame(const QImage &frame); 85 | signals: 86 | void newImageProcessingFlags(struct ImageProcessingFlags imageProcessingFlags); 87 | void newTaskData(struct TaskData taskData); 88 | }; 89 | 90 | #endif // MAINWINDOW_H 91 | -------------------------------------------------------------------------------- /MainWindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 671 10 | 654 11 | 12 | 13 | 14 | 15 | 0 16 | 0 17 | 18 | 19 | 20 | 21 | 661 22 | 633 23 | 24 | 25 | 26 | qt-opencv-multithreaded 27 | 28 | 29 | false 30 | 31 | 32 | 33 | 34 | 35 | 10 36 | 10 37 | 651 38 | 611 39 | 40 | 41 | 42 | 43 | 44 | 45 | true 46 | 47 | 48 | 49 | 0 50 | 0 51 | 52 | 53 | 54 | 55 | 642 56 | 482 57 | 58 | 59 | 60 | true 61 | 62 | 63 | true 64 | 65 | 66 | QFrame::Box 67 | 68 | 69 | QFrame::Raised 70 | 71 | 72 | 1 73 | 74 | 75 | Qt::AlignCenter 76 | 77 | 78 | 79 | 80 | 81 | 82 | Qt::Horizontal 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 180 97 | 0 98 | 99 | 100 | 101 | 102 | 180 103 | 16777215 104 | 105 | 106 | 107 | 108 | 8 109 | 75 110 | true 111 | 112 | 113 | 114 | Camera Device Number: 115 | 116 | 117 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 8 126 | 127 | 128 | 129 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 180 142 | 0 143 | 144 | 145 | 146 | 147 | 180 148 | 16777215 149 | 150 | 151 | 152 | 153 | 8 154 | 75 155 | true 156 | 157 | 158 | 159 | Camera Resolution: 160 | 161 | 162 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 8 171 | 172 | 173 | 174 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 180 187 | 0 188 | 189 | 190 | 191 | 192 | 180 193 | 16777215 194 | 195 | 196 | 197 | 198 | 8 199 | 75 200 | true 201 | 202 | 203 | 204 | Image Buffer Status (% full): 205 | 206 | 207 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 8 216 | 217 | 218 | 219 | 0 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 60 228 | 0 229 | 230 | 231 | 232 | 233 | 60 234 | 16777215 235 | 236 | 237 | 238 | 239 | 8 240 | 241 | 242 | 243 | 244 | 245 | 246 | Qt::AlignCenter 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 8 257 | 258 | 259 | 260 | Clear Image Buffer 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | Qt::Horizontal 270 | 271 | 272 | 273 | 40 274 | 20 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 110 288 | 0 289 | 290 | 291 | 292 | 293 | 110 294 | 16777215 295 | 296 | 297 | 298 | 299 | 8 300 | 75 301 | true 302 | 303 | 304 | 305 | Capture Rate: 306 | 307 | 308 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 309 | 310 | 311 | 312 | 313 | 314 | 315 | true 316 | 317 | 318 | 319 | 140 320 | 0 321 | 322 | 323 | 324 | 325 | 140 326 | 16777215 327 | 328 | 329 | 330 | 331 | 8 332 | 333 | 334 | 335 | 336 | 337 | 338 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 110 351 | 0 352 | 353 | 354 | 355 | 356 | 110 357 | 16777215 358 | 359 | 360 | 361 | 362 | 8 363 | 75 364 | true 365 | 366 | 367 | 368 | Processing Rate: 369 | 370 | 371 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 140 380 | 0 381 | 382 | 383 | 384 | 385 | 140 386 | 16777215 387 | 388 | 389 | 390 | 391 | 8 392 | 393 | 394 | 395 | 396 | 397 | 398 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 110 411 | 0 412 | 413 | 414 | 415 | 416 | 110 417 | 16777215 418 | 419 | 420 | 421 | 422 | 8 423 | 75 424 | true 425 | 426 | 427 | 428 | ROI: 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 140 437 | 0 438 | 439 | 440 | 441 | 442 | 140 443 | 16777215 444 | 445 | 446 | 447 | 448 | 8 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 110 465 | 0 466 | 467 | 468 | 469 | 470 | 110 471 | 16777215 472 | 473 | 474 | 475 | 476 | 8 477 | 75 478 | true 479 | 480 | 481 | 482 | Cursor 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 140 491 | 0 492 | 493 | 494 | 495 | 496 | 140 497 | 16777215 498 | 499 | 500 | 501 | 502 | 8 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | Qt::Horizontal 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 0 530 | 0 531 | 671 532 | 23 533 | 534 | 535 | 536 | 537 | Main 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | Help 547 | 548 | 549 | 550 | 551 | 552 | Image Processing 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | Connect to Camera... 570 | 571 | 572 | 573 | 574 | Disconnect Camera 575 | 576 | 577 | 578 | 579 | Exit 580 | 581 | 582 | 583 | 584 | About... 585 | 586 | 587 | 588 | 589 | true 590 | 591 | 592 | 1: Grayscale 593 | 594 | 595 | 596 | 597 | true 598 | 599 | 600 | 2: Smooth 601 | 602 | 603 | 604 | 605 | true 606 | 607 | 608 | 5: Flip 609 | 610 | 611 | 612 | 613 | true 614 | 615 | 616 | 3: Dilate 617 | 618 | 619 | 620 | 621 | true 622 | 623 | 624 | 4: Erode 625 | 626 | 627 | 628 | 629 | true 630 | 631 | 632 | 6. Canny 633 | 634 | 635 | 636 | 637 | Settings... 638 | 639 | 640 | 641 | 642 | 643 | FrameLabel 644 | QLabel 645 |
FrameLabel.h
646 |
647 |
648 | 649 | 650 |
651 | -------------------------------------------------------------------------------- /MatToQImage.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* MatToQImage.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "MatToQImage.h" 34 | 35 | QImage MatToQImage(const Mat& mat) 36 | { 37 | // 8-bits unsigned, NO. OF CHANNELS=1 38 | if(mat.type()==CV_8UC1) 39 | { 40 | // Set the color table (used to translate colour indexes to qRgb values) 41 | QVector colorTable; 42 | for (int i=0; i<256; i++) 43 | colorTable.push_back(qRgb(i,i,i)); 44 | // Copy input Mat 45 | const uchar *qImageBuffer = (const uchar*)mat.data; 46 | // Create QImage with same dimensions as input Mat 47 | QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8); 48 | img.setColorTable(colorTable); 49 | return img; 50 | } 51 | // 8-bits unsigned, NO. OF CHANNELS=3 52 | if(mat.type()==CV_8UC3) 53 | { 54 | // Copy input Mat 55 | const uchar *qImageBuffer = (const uchar*)mat.data; 56 | // Create QImage with same dimensions as input Mat 57 | QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888); 58 | return img.rgbSwapped(); 59 | } 60 | else 61 | { 62 | qDebug() << "ERROR: Mat could not be converted to QImage."; 63 | return QImage(); 64 | } 65 | } // MatToQImage() 66 | -------------------------------------------------------------------------------- /MatToQImage.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* MatToQImage.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef MATTOQIMAGE_H 34 | #define MATTOQIMAGE_H 35 | 36 | // Qt header files 37 | #include 38 | #include 39 | // OpenCV header files 40 | #include 41 | #include 42 | 43 | using namespace cv; 44 | 45 | QImage MatToQImage(const Mat&); 46 | 47 | #endif // MATTOQIMAGE_H 48 | -------------------------------------------------------------------------------- /ProcessingThread.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* ProcessingThread.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "ImageBuffer.h" 34 | #include "ProcessingThread.h" 35 | #include "MatToQImage.h" 36 | 37 | // Qt header files 38 | #include 39 | // OpenCV header files 40 | #include 41 | #include 42 | // Configuration header file 43 | #include "Config.h" 44 | 45 | ProcessingThread::ProcessingThread(ImageBuffer *imageBuffer, int inputSourceWidth, int inputSourceHeight) : QThread(), 46 | imageBuffer(imageBuffer), 47 | inputSourceWidth(inputSourceWidth), 48 | inputSourceHeight(inputSourceHeight) 49 | { 50 | // Initialize variables 51 | stopped=false; 52 | sampleNo=0; 53 | fpsSum=0; 54 | avgFPS=0; 55 | fps.clear(); 56 | currentROI=Rect(0,0,inputSourceWidth,inputSourceHeight); 57 | // Store original ROI 58 | originalROI=currentROI; 59 | } // ProcessingThread constructor 60 | 61 | ProcessingThread::~ProcessingThread() 62 | { 63 | } // ProcessingThread destructor 64 | 65 | void ProcessingThread::run() 66 | { 67 | while(1) 68 | { 69 | ///////////////////////////////// 70 | // Stop thread if stopped=TRUE // 71 | ///////////////////////////////// 72 | stoppedMutex.lock(); 73 | if (stopped) 74 | { 75 | stopped=false; 76 | stoppedMutex.unlock(); 77 | break; 78 | } 79 | stoppedMutex.unlock(); 80 | ///////////////////////////////// 81 | ///////////////////////////////// 82 | 83 | // Save processing time 84 | processingTime=t.elapsed(); 85 | // Start timer (used to calculate processing rate) 86 | t.start(); 87 | 88 | // Get frame from queue, store in currentFrame, set ROI 89 | currentFrame=Mat(imageBuffer->getFrame(),currentROI); 90 | 91 | updateMembersMutex.lock(); 92 | /////////////////// 93 | // PERFORM TASKS // 94 | /////////////////// 95 | // Note: ROI changes will take effect on next frame 96 | if(resetROIFlag) 97 | resetROI(); 98 | else if(setROIFlag) 99 | setROI(); 100 | //////////////////////////////////// 101 | // PERFORM IMAGE PROCESSING BELOW // 102 | //////////////////////////////////// 103 | // Grayscale conversion 104 | if(grayscaleOn) 105 | cvtColor(currentFrame,currentFrameGrayscale,CV_BGR2GRAY); 106 | // Smooth (in-place operations) 107 | if(smoothOn) 108 | { 109 | if(grayscaleOn) 110 | { 111 | switch(smoothType) 112 | { 113 | // BLUR 114 | case 0: 115 | blur(currentFrameGrayscale,currentFrameGrayscale,Size(smoothParam1,smoothParam2)); 116 | break; 117 | // GAUSSIAN 118 | case 1: 119 | GaussianBlur(currentFrameGrayscale,currentFrameGrayscale,Size(smoothParam1,smoothParam2),smoothParam3,smoothParam4); 120 | break; 121 | // MEDIAN 122 | case 2: 123 | medianBlur(currentFrameGrayscale,currentFrameGrayscale,smoothParam1); 124 | break; 125 | } 126 | } 127 | else 128 | { 129 | switch(smoothType) 130 | { 131 | // BLUR 132 | case 0: 133 | blur(currentFrame,currentFrame,Size(smoothParam1,smoothParam2)); 134 | break; 135 | // GAUSSIAN 136 | case 1: 137 | GaussianBlur(currentFrame,currentFrame,Size(smoothParam1,smoothParam2),smoothParam3,smoothParam4); 138 | break; 139 | // MEDIAN 140 | case 2: 141 | medianBlur(currentFrame,currentFrame,smoothParam1); 142 | break; 143 | } 144 | } 145 | } 146 | // Dilate 147 | if(dilateOn) 148 | { 149 | if(grayscaleOn) 150 | dilate(currentFrameGrayscale,currentFrameGrayscale,Mat(),Point(-1,-1),dilateNumberOfIterations); 151 | else 152 | dilate(currentFrame,currentFrame,Mat(),Point(-1,-1),dilateNumberOfIterations); 153 | } 154 | // Erode 155 | if(erodeOn) 156 | { 157 | if(grayscaleOn) 158 | erode(currentFrameGrayscale,currentFrameGrayscale,Mat(),Point(-1,-1),erodeNumberOfIterations); 159 | else 160 | erode(currentFrame,currentFrame,Mat(),Point(-1,-1),erodeNumberOfIterations); 161 | } 162 | // Flip 163 | if(flipOn) 164 | { 165 | if(grayscaleOn) 166 | flip(currentFrameGrayscale,currentFrameGrayscale,flipCode); 167 | else 168 | flip(currentFrame,currentFrame,flipCode); 169 | } 170 | // Canny edge detection 171 | if(cannyOn) 172 | { 173 | if(!grayscaleOn) 174 | Canny(currentFrame,currentFrame, 175 | cannyThreshold1,cannyThreshold2, 176 | cannyApertureSize,cannyL2gradient); 177 | else 178 | Canny(currentFrameGrayscale,currentFrameGrayscale, 179 | cannyThreshold1,cannyThreshold2, 180 | cannyApertureSize,cannyL2gradient); 181 | } 182 | //////////////////////////////////// 183 | // PERFORM IMAGE PROCESSING ABOVE // 184 | //////////////////////////////////// 185 | 186 | // Convert Mat to QImage: Show grayscale frame [if Grayscale mode is ON] 187 | if(grayscaleOn) 188 | frame=MatToQImage(currentFrameGrayscale); 189 | // Convert Mat to QImage: Show BGR frame 190 | else 191 | frame=MatToQImage(currentFrame); 192 | updateMembersMutex.unlock(); 193 | 194 | // Update statistics 195 | updateFPS(processingTime); 196 | currentSizeOfBuffer=imageBuffer->getSizeOfImageBuffer(); 197 | // Inform GUI thread of new frame (QImage) 198 | emit newFrame(frame); 199 | } 200 | qDebug() << "Stopping processing thread..."; 201 | } 202 | 203 | void ProcessingThread::updateFPS(int timeElapsed) 204 | { 205 | // Add instantaneous FPS value to queue 206 | if(timeElapsed>0) 207 | { 208 | fps.enqueue((int)1000/timeElapsed); 209 | // Increment sample number 210 | sampleNo++; 211 | } 212 | // Maximum size of queue is DEFAULT_PROCESSING_FPS_STAT_QUEUE_LENGTH 213 | if(fps.size()>PROCESSING_FPS_STAT_QUEUE_LENGTH) 214 | fps.dequeue(); 215 | // Update FPS value every DEFAULT_PROCESSING_FPS_STAT_QUEUE_LENGTH samples 216 | if((fps.size()==PROCESSING_FPS_STAT_QUEUE_LENGTH)&&(sampleNo==PROCESSING_FPS_STAT_QUEUE_LENGTH)) 217 | { 218 | // Empty queue and store sum 219 | while(!fps.empty()) 220 | fpsSum+=fps.dequeue(); 221 | // Calculate average FPS 222 | avgFPS=fpsSum/PROCESSING_FPS_STAT_QUEUE_LENGTH; 223 | // Reset sum 224 | fpsSum=0; 225 | // Reset sample number 226 | sampleNo=0; 227 | } 228 | } // updateFPS() 229 | 230 | void ProcessingThread::stopProcessingThread() 231 | { 232 | stoppedMutex.lock(); 233 | stopped=true; 234 | stoppedMutex.unlock(); 235 | } // stopProcessingThread() 236 | 237 | void ProcessingThread::setROI() 238 | { 239 | // Save selection as new (current) ROI 240 | currentROI=selectionBox; 241 | qDebug() << "ROI successfully SET."; 242 | // Reset flag to FALSE 243 | setROIFlag=false; 244 | } // setROI() 245 | 246 | void ProcessingThread::resetROI() 247 | { 248 | // Reset ROI to original if not already 249 | if((currentROI.x!=originalROI.x)&&(currentROI.y!=originalROI.y)&&(currentROI.width!=originalROI.width)&&(currentROI.height!=originalROI.height)) 250 | { 251 | currentROI=originalROI; 252 | qDebug() << "ROI successfully RESET."; 253 | } 254 | // Reset flag to FALSE 255 | resetROIFlag=false; 256 | } // resetROI() 257 | 258 | void ProcessingThread::updateImageProcessingFlags(struct ImageProcessingFlags imageProcessingFlags) 259 | { 260 | QMutexLocker locker(&updateMembersMutex); 261 | this->grayscaleOn=imageProcessingFlags.grayscaleOn; 262 | this->smoothOn=imageProcessingFlags.smoothOn; 263 | this->dilateOn=imageProcessingFlags.dilateOn; 264 | this->erodeOn=imageProcessingFlags.erodeOn; 265 | this->flipOn=imageProcessingFlags.flipOn; 266 | this->cannyOn=imageProcessingFlags.cannyOn; 267 | } // updateImageProcessingFlags() 268 | 269 | void ProcessingThread::updateImageProcessingSettings(struct ImageProcessingSettings imageProcessingSettings) 270 | { 271 | QMutexLocker locker(&updateMembersMutex); 272 | this->smoothType=imageProcessingSettings.smoothType; 273 | this->smoothParam1=imageProcessingSettings.smoothParam1; 274 | this->smoothParam2=imageProcessingSettings.smoothParam2; 275 | this->smoothParam3=imageProcessingSettings.smoothParam3; 276 | this->smoothParam4=imageProcessingSettings.smoothParam4; 277 | this->dilateNumberOfIterations=imageProcessingSettings.dilateNumberOfIterations; 278 | this->erodeNumberOfIterations=imageProcessingSettings.erodeNumberOfIterations; 279 | this->flipCode=imageProcessingSettings.flipCode; 280 | this->cannyThreshold1=imageProcessingSettings.cannyThreshold1; 281 | this->cannyThreshold2=imageProcessingSettings.cannyThreshold2; 282 | this->cannyApertureSize=imageProcessingSettings.cannyApertureSize; 283 | this->cannyL2gradient=imageProcessingSettings.cannyL2gradient; 284 | } // updateImageProcessingSettings() 285 | 286 | void ProcessingThread::updateTaskData(struct TaskData taskData) 287 | { 288 | QMutexLocker locker(&updateMembersMutex); 289 | this->setROIFlag=taskData.setROIFlag; 290 | this->resetROIFlag=taskData.resetROIFlag; 291 | this->selectionBox.x=taskData.selectionBox.left(); 292 | this->selectionBox.y=taskData.selectionBox.top(); 293 | this->selectionBox.width=taskData.selectionBox.width(); 294 | this->selectionBox.height=taskData.selectionBox.height(); 295 | } // updateTaskData() 296 | 297 | int ProcessingThread::getAvgFPS() 298 | { 299 | return avgFPS; 300 | } // getAvgFPS() 301 | 302 | int ProcessingThread::getCurrentSizeOfBuffer() 303 | { 304 | return currentSizeOfBuffer; 305 | } // getCurrentSizeOfBuffer() 306 | 307 | Rect ProcessingThread::getCurrentROI() 308 | { 309 | return currentROI; 310 | } // getCurrentROI(); 311 | -------------------------------------------------------------------------------- /ProcessingThread.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* ProcessingThead.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef PROCESSINGTHREAD_H 34 | #define PROCESSINGTHREAD_H 35 | 36 | #include "Structures.h" 37 | 38 | // Qt header files 39 | #include 40 | #include 41 | // OpenCV header files 42 | #include 43 | 44 | using namespace cv; 45 | 46 | class ImageBuffer; 47 | 48 | class ProcessingThread : public QThread 49 | { 50 | Q_OBJECT 51 | 52 | public: 53 | ProcessingThread(ImageBuffer *imageBuffer, int inputSourceWidth, int inputSourceHeight); 54 | ~ProcessingThread(); 55 | void stopProcessingThread(); 56 | int getAvgFPS(); 57 | int getCurrentSizeOfBuffer(); 58 | Rect getCurrentROI(); 59 | private: 60 | void updateFPS(int); 61 | void setROI(); 62 | void resetROI(); 63 | ImageBuffer *imageBuffer; 64 | volatile bool stopped; 65 | int inputSourceWidth; 66 | int inputSourceHeight; 67 | int currentSizeOfBuffer; 68 | Mat currentFrame; 69 | Mat currentFrameGrayscale; 70 | Rect originalROI; 71 | Rect currentROI; 72 | QImage frame; 73 | QTime t; 74 | int processingTime; 75 | QQueue fps; 76 | int fpsSum; 77 | int sampleNo; 78 | int avgFPS; 79 | QMutex stoppedMutex; 80 | QMutex updateMembersMutex; 81 | Size frameSize; 82 | Point framePoint; 83 | // Image processing flags 84 | bool grayscaleOn; 85 | bool smoothOn; 86 | bool dilateOn; 87 | bool erodeOn; 88 | bool flipOn; 89 | bool cannyOn; 90 | // Image processing settings 91 | int smoothType; 92 | int smoothParam1; 93 | int smoothParam2; 94 | double smoothParam3; 95 | double smoothParam4; 96 | int dilateNumberOfIterations; 97 | int erodeNumberOfIterations; 98 | int flipCode; 99 | double cannyThreshold1; 100 | double cannyThreshold2; 101 | int cannyApertureSize; 102 | bool cannyL2gradient; 103 | // Task data 104 | bool setROIFlag; 105 | bool resetROIFlag; 106 | Rect selectionBox; 107 | protected: 108 | void run(); 109 | private slots: 110 | void updateImageProcessingFlags(struct ImageProcessingFlags); 111 | void updateImageProcessingSettings(struct ImageProcessingSettings); 112 | void updateTaskData(struct TaskData); 113 | signals: 114 | void newFrame(const QImage &frame); 115 | }; 116 | 117 | #endif // PROCESSINGTHREAD_H 118 | -------------------------------------------------------------------------------- /Structures.h: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* Structures.h */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #ifndef STRUCTURES_H 34 | #define STRUCTURES_H 35 | 36 | // Qt header files 37 | #include 38 | 39 | // ImageProcessingSettings structure definition 40 | struct ImageProcessingSettings{ 41 | int smoothType; 42 | int smoothParam1; 43 | int smoothParam2; 44 | double smoothParam3; 45 | double smoothParam4; 46 | int dilateNumberOfIterations; 47 | int erodeNumberOfIterations; 48 | int flipCode; 49 | double cannyThreshold1; 50 | double cannyThreshold2; 51 | int cannyApertureSize; 52 | bool cannyL2gradient; 53 | }; 54 | 55 | // ImageProcessingFlags structure definition 56 | struct ImageProcessingFlags{ 57 | bool grayscaleOn; 58 | bool smoothOn; 59 | bool dilateOn; 60 | bool erodeOn; 61 | bool flipOn; 62 | bool cannyOn; 63 | }; 64 | 65 | // TaskData structure definition 66 | struct TaskData{ 67 | QRect selectionBox; 68 | bool setROIFlag; 69 | bool resetROIFlag; 70 | }; 71 | 72 | // MouseData structure definition 73 | struct MouseData{ 74 | QRect selectionBox; 75 | bool leftButtonRelease; 76 | bool rightButtonRelease; 77 | }; 78 | 79 | #endif // STRUCTURES_H 80 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************/ 2 | /* qt-opencv-multithreaded: */ 3 | /* A multithreaded OpenCV application using the Qt framework. */ 4 | /* */ 5 | /* main.cpp */ 6 | /* */ 7 | /* Nick D'Ademo */ 8 | /* */ 9 | /* Copyright (c) 2012 Nick D'Ademo */ 10 | /* */ 11 | /* Permission is hereby granted, free of charge, to any person */ 12 | /* obtaining a copy of this software and associated documentation */ 13 | /* files (the "Software"), to deal in the Software without restriction, */ 14 | /* including without limitation the rights to use, copy, modify, merge, */ 15 | /* publish, distribute, sublicense, and/or sell copies of the Software, */ 16 | /* and to permit persons to whom the Software is furnished to do so, */ 17 | /* subject to the following conditions: */ 18 | /* */ 19 | /* The above copyright notice and this permission notice shall be */ 20 | /* included in all copies or substantial portions of the Software. */ 21 | /* */ 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ 25 | /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */ 26 | /* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */ 27 | /* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */ 28 | /* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ 29 | /* SOFTWARE. */ 30 | /* */ 31 | /************************************************************************/ 32 | 33 | #include "MainWindow.h" 34 | 35 | // Qt header files 36 | #include 37 | 38 | #define X_INITIAL 0 39 | #define Y_INITIAL 0 40 | 41 | int main(int argc, char *argv[]) 42 | { 43 | QApplication a(argc, argv); 44 | a.setApplicationVersion(QUOTE(APP_VERSION)); 45 | MainWindow w; 46 | w.show(); 47 | // Set the initial screen position of the main window 48 | w.setGeometry(X_INITIAL, Y_INITIAL, w.width(), w.height()); 49 | return a.exec(); 50 | } // main() 51 | -------------------------------------------------------------------------------- /qt-opencv-multithreaded.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | TARGET = qt-opencv-multithreaded 4 | TEMPLATE = app 5 | 6 | VERSION = 1.22 7 | 8 | DEFINES += APP_VERSION=$$VERSION 9 | 10 | FORMS = ImageProcessingSettingsDialog.ui CameraConnectDialog.ui MainWindow.ui 11 | 12 | SOURCES += main.cpp\ 13 | MainWindow.cpp \ 14 | CaptureThread.cpp \ 15 | Controller.cpp \ 16 | ImageBuffer.cpp \ 17 | CameraConnectDialog.cpp \ 18 | ProcessingThread.cpp \ 19 | FrameLabel.cpp \ 20 | MatToQImage.cpp \ 21 | ImageProcessingSettingsDialog.cpp 22 | 23 | HEADERS += MainWindow.h \ 24 | CaptureThread.h \ 25 | Controller.h \ 26 | ImageBuffer.h \ 27 | CameraConnectDialog.h \ 28 | ProcessingThread.h \ 29 | FrameLabel.h \ 30 | Structures.h \ 31 | Config.h \ 32 | MatToQImage.h \ 33 | ImageProcessingSettingsDialog.h 34 | 35 | LIBS += -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann 36 | --------------------------------------------------------------------------------