Results 1 to 5 of 5

Thread: slider and color filtering or thresholding

  1. #1
    Join Date
    Oct 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default slider and color filtering or thresholding

    hello guys,


    i want to make an app to track an Object.

    I am using opencv. I want change color threshold values using QSliders. However, i am unable to do so.
    Please see attached pic along with the code.

    Notice all the zeros in plainTextedit. Even if i change the slider position my variables ch1_min and others change. However, i still have 0 numbers for this in inRange function in the code.

    Please help.

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. //////////////////////////////////////////////////////////////////////////
    5. /// \brief -> this is the Constructor
    6. /// \param parent
    7. //////////////////////////////////////////////////////////////////////////
    8.  
    9. Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
    10. {
    11. ui->setupUi(this);
    12. timer1 = new QTimer(this);
    13. connect(timer1, SIGNAL(timeout()), this, SLOT(ShowCamera()));
    14. ch1_min = ch2_min = ch3_min = 0;
    15. ch1_max = ch2_max = ch3_max = 255;
    16. }
    17. //////////////////////////////////////////////////////////////////////////
    18. /// \brief -> this is the Deconstructor
    19. //////////////////////////////////////////////////////////////////////////
    20. Dialog::~Dialog()
    21. {
    22. delete ui;
    23. }
    24. /////////////////////////////////////////////////////////////////////////////
    25. /// \brief Dialog::Dialog::paintEvent -> putText function
    26. /// \param e
    27. /////////////////////////////////////////////////////////////////////////////
    28. /*void Dialog::putText(QPaintEvent *e)
    29. {
    30.   QPainter painter(&tempframe);
    31.   QPen ourpen(Qt::magenta);
    32.   ourpen.setWidth(2);
    33.   painter.setPen(ourpen);
    34.   painter.setFont(QFont("Times", 20, QFont::Bold));
    35.   painter.drawText(outputView.rect(), "THIS!!"); ////////////// doesn't work
    36. }*/
    37. //////////////////////////////////////////////////////////////////////////////////////////////
    38. /// \brief Dialog::on_dispButton_clicked -> this function starts or stops the camera.
    39. ///
    40. ///
    41. /////////////////////////////////////////////////////////////////////////////////////////////
    42.  
    43. void Dialog::on_dispButton_clicked()
    44. {
    45. if(timer1->isActive() == true)
    46. {
    47. ui->dispButton->setText("Start camView");
    48. timer1->stop();
    49. cameraFeed.release();
    50. ui->camView->setText("Your camera view will be available here.");
    51.  
    52. }
    53. else
    54. {
    55. ui->dispButton->setText("Stop camView");
    56. timer1->start(30);
    57. Camera.open(0);
    58.  
    59. }
    60.  
    61. }
    62.  
    63.  
    64.  
    65. //////////////////////////////////////////////////////////////////////////////////////////////
    66. /// \brief all sliderMoved to change channel min and max
    67. /// \param ch1_min ch2_min ch3_min ch1_max ch2_max ch3_max
    68. /////////////////////////////////////////////////////////////////////////////////////////////
    69. void Dialog::on_ch1min_sliderMoved(int ch1_min)
    70. {
    71. ui->ch1min->valueChanged(ch1_min);
    72. if(ch1_min < ch1_max)
    73. {
    74.  
    75. ch1_min_string = QString::number(ch1_min);
    76. ui->dsp_ch1_min->clear();
    77. ui->dsp_ch1_min->appendPlainText(ch1_min_string);
    78. }
    79. else
    80. {
    81. ch1_min = ch1_max-1;
    82. ui->ch1min->valueChanged(ch1_min);
    83. ch1_min_string = QString::number(ch1_min);
    84. ui->dsp_ch1_min->clear();
    85. ui->dsp_ch1_min->appendPlainText(ch1_min_string);
    86. }
    87. }
    88.  
    89. void Dialog::on_ch2min_sliderMoved(int ch2_min)
    90. {
    91. ui->ch2min->valueChanged(ch2_min);
    92. if(ch2_min < ch2_max)
    93. {
    94.  
    95. ch2_min_string = QString::number(ch2_min);
    96. ui->dsp_ch2_min->clear();
    97. ui->dsp_ch2_min->appendPlainText(ch2_min_string);
    98. }
    99. else
    100. {
    101. ch2_min = ch2_max-1;
    102. ui->ch2min->valueChanged(ch2_min);
    103. ch2_min_string = QString::number(ch2_min);
    104. ui->dsp_ch2_min->clear();
    105. ui->dsp_ch2_min->appendPlainText(ch2_min_string);
    106. }
    107. }
    108.  
    109. void Dialog::on_ch3min_sliderMoved(int ch3_min)
    110. {
    111. ui->ch3min->valueChanged(ch3_min);
    112. if(ch3_min < ch3_max)
    113. {
    114.  
    115. ch3_min_string = QString::number(ch3_min);
    116. ui->dsp_ch3_min->clear();
    117. ui->dsp_ch3_min->appendPlainText(ch3_min_string);
    118. }
    119. else
    120. {
    121. ch3_min = ch3_max-1;
    122. ui->ch3min->valueChanged(ch3_min);
    123. ch3_min_string = QString::number(ch3_min);
    124. ui->dsp_ch3_min->clear();
    125. ui->dsp_ch3_min->appendPlainText(ch3_min_string);
    126. }
    127. }
    128.  
    129. void Dialog::on_ch1max_sliderMoved(int ch1_max)
    130. {
    131. ui->ch1max->valueChanged(ch1_max);
    132. if(ch1_max > ch1_min)
    133. {
    134. ch1_max_string = QString::number(ch1_max);
    135. ui->dsp_ch1_max->clear();
    136. ui->dsp_ch1_max->appendPlainText(ch1_max_string);
    137. }
    138. else
    139. {
    140. ch1_max= ch1_min+1;
    141. ch1_max_string = QString::number(ch1_max);
    142. ui->dsp_ch1_max->clear();
    143. ui->dsp_ch1_max->appendPlainText(ch1_max_string);
    144. }
    145.  
    146. }
    147.  
    148. void Dialog::on_ch2max_sliderMoved(int ch2_max)
    149. {
    150. ui->ch2max->valueChanged(ch2_max);
    151. if(ch2_max>ch2_min)
    152. {
    153. ch2_max_string = QString::number(ch2_max);
    154. ui->dsp_ch2_max->clear();
    155. ui->dsp_ch2_max->appendPlainText(ch2_max_string);
    156. }
    157. else
    158. {
    159. ch2_max = ch2_min+1;
    160. ch2_max_string = QString::number(ch2_max);
    161. ui->dsp_ch2_max->clear();
    162. ui->dsp_ch2_max->appendPlainText(ch2_max_string);
    163. }
    164. }
    165.  
    166. void Dialog::on_ch3max_sliderMoved(int ch3_max)
    167. {
    168. ui->ch3max->valueChanged(ch3_max);
    169. if(ch3_max>ch3_min)
    170. {
    171. ch3_max_string = QString::number(ch3_max);
    172. ui->dsp_ch3_max->clear();
    173. ui->dsp_ch3_max->appendPlainText(ch3_max_string);
    174. }
    175. else
    176. {
    177. ch3_max=ch3_min+1;
    178. ch3_max_string = QString::number(ch3_max);
    179. ui->dsp_ch3_max->clear();
    180. ui->dsp_ch3_max->appendPlainText(ch3_max_string);
    181. }
    182. }
    183.  
    184. ////////////////////////////////////////////////////////////////////////////////////////////////
    185. /// \brief Dialog::ShowCamera -> here we read frames and show them in label called camView
    186. /// temp is used only to make use of visual representation of image in Qt using QImage.
    187. ///
    188. ////////////////////////////////////////////////////////////////////////////////////////////////
    189. void Dialog::ShowCamera()
    190. {
    191. Camera.read(cameraFeed);
    192.  
    193. ui->plainTextEdit->appendPlainText((QString::number(ch1_min))); ////this is the reason of all the zeros in plainTextEdit. for this function changed values do not appear updated
    194.  
    195. cv::inRange(cameraFeed,cv::Scalar(ch1_min,ch2_min,ch3_min),cv::Scalar(ch1_max,ch2_max,ch3_max),thresholdFeed);
    196. if(cameraFeed.empty() == true) return;
    197.  
    198. ////////////////// visualization of image acquired from camera
    199.  
    200. cameraFeed.copyTo(temp);
    201. cv::cvtColor(temp, temp, CV_BGR2RGB);
    202. QImage qimgOriginal((uchar*) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
    203.  
    204.  
    205. ////////////////// visualization of binary image
    206.  
    207. QImage qimgProcessed((uchar*) thresholdFeed.data, thresholdFeed.cols, thresholdFeed.rows, thresholdFeed.step, QImage::Format_Indexed8);
    208. ui->camView->setPixmap(QPixmap::fromImage(qimgOriginal));
    209. ui->outputView->setPixmap(QPixmap::fromImage(qimgProcessed));
    210.  
    211.  
    212.  
    213. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: slider and color filtering or thresholding

    valueChanged() is a signal and not a method for reading values from widgets. You should simply connect the valueChanged signal from a slider to the setValue slot from the spinbox and vice versa. Then connect to the valueChanged signal of one of those widgets to a custom slot accepting an integer which is going to contain the value you can use directly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: slider and color filtering or thresholding

    Quote Originally Posted by wysota View Post
    Then connect to the valueChanged signal of one of those widgets to a custom slot accepting an integer which is going to contain the value you can use directly.

    Ohhh yes. I understand however.
    I am not able to write
    connect(ch1min, SIGNAL(valuechanged(int)), this, changeval1(int));

    Coz i do not have ch1min available in dialog.cpp because i have draen sliders using gui of qt. Further i do not have change1val(int) available in gui design so that i can add a signal anx slot connection.

    Please help.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: slider and color filtering or thresholding

    Quote Originally Posted by rcmandar View Post
    because i have draen sliders using gui of qt.
    I don't see how that matters.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: slider and color filtering or thresholding

    i defined another set of variables and did rmin =ch1_min; when sliderMoved or spinBoxvalueChanged.

    however i am now baffled by this opencv error:
    Qt Code:
    1. OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/madmandy/opencv-2.4.9/modules/core/src/array.cpp, line 2482
    2. terminate called after throwing an instance of 'cv::Exception'
    3. what(): /home/madmandy/opencv-2.4.9/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat
    To copy to clipboard, switch view to plain text mode 

    it is due to function trackLP. please help. i tried everything else form google searches.

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. //////////////////////////////////////////////////////////////////////////
    5. /// \brief -> this is the Constructor
    6. /// \param parent
    7. //////////////////////////////////////////////////////////////////////////
    8.  
    9. Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
    10. {
    11. ui->setupUi(this);
    12. timer1 = new QTimer(this);
    13. connect(timer1, SIGNAL(timeout()), this, SLOT(ShowCamera()));
    14. ch1_min = ch2_min = ch3_min = 0;
    15. ch1_max = ch2_max = ch3_max = 255;
    16. rmin = gmin = bmin = 0;
    17. rmax = gmax = bmax = 255;
    18. }
    19. //////////////////////////////////////////////////////////////////////////
    20. /// \brief -> this is the Deconstructor
    21. //////////////////////////////////////////////////////////////////////////
    22. Dialog::~Dialog()
    23. {
    24. delete ui;
    25. }
    26.  
    27. /*
    28. /////////////////////////////////////////////////////////////////////////////
    29. /// \brief Dialog::Dialog::paintEvent -> putText function
    30. /// \param e
    31. /////////////////////////////////////////////////////////////////////////////
    32. void Dialog::putText(QPaintEvent *e)
    33. {
    34.   QPainter painter(&tempframe);
    35.   QPen ourpen(Qt::magenta);
    36.   ourpen.setWidth(2);
    37.   painter.setPen(ourpen);
    38.   painter.setFont(QFont("Times", 20, QFont::Bold));
    39.   painter.drawText(outputView.rect(), "THIS!!");
    40. }*/
    41. //////////////////////////////////////////////////////////////////////////////////////////////
    42. /// \brief Dialog::on_dispButton_clicked -> this function starts or stops the camera.
    43. ///
    44. ///
    45. /////////////////////////////////////////////////////////////////////////////////////////////
    46.  
    47. void Dialog::on_dispButton_clicked()
    48. {
    49. if(timer1->isActive() == true)
    50. {
    51. ui->dispButton->setText("Start camView");
    52. timer1->stop();
    53. cameraFeed.release();
    54. ui->camView->setText("Your camera view will be available here.");
    55. thresholdFeed.release();
    56. ui->outputView->setText("Your output view will be available here.");
    57.  
    58. }
    59. else
    60. {
    61. ui->dispButton->setText("Stop camView");
    62. timer1->start(30);
    63. Camera.open(1); ///// 1 -> usb webcam
    64.  
    65. }
    66.  
    67. }
    68. //////////////////////////////////////////////////////////////////////////////////////////////
    69. /// \brief all sliderMoved and spinBox value changed to change channel min and max
    70. /// \param ch1_min ch2_min ch3_min ch1_max ch2_max ch3_max
    71. /// \param rmin gmin bmin rmax gmax bmax
    72. /////////////////////////////////////////////////////////////////////////////////////////////
    73.  
    74.  
    75. void Dialog::on_ch1min_sliderMoved(int ch1_min)
    76. {
    77.  
    78. rmin = ch1_min;
    79. }
    80.  
    81.  
    82. void Dialog::on_ch1minspinBox_valueChanged(int ch1_min)
    83. {
    84. rmin = ch1_min;
    85. }
    86.  
    87.  
    88.  
    89. void Dialog::on_ch2min_valueChanged(int ch2_min)
    90. {
    91. gmin = ch2_min;
    92. }
    93.  
    94. void Dialog::on_ch2minspinBox_valueChanged(int ch2_min)
    95. {
    96. gmin = ch2_min;
    97. }
    98.  
    99. void Dialog::on_ch3min_valueChanged(int ch3_min)
    100. {
    101. bmin = ch3_min;
    102. }
    103.  
    104. void Dialog::on_ch3minspinBox_valueChanged(int ch3_min)
    105. {
    106. bmin = ch3_min;
    107. }
    108.  
    109. void Dialog::on_ch1max_valueChanged(int ch1_max)
    110. {
    111. rmax = ch1_max;
    112. }
    113.  
    114. void Dialog::on_ch1maxspinBox_valueChanged(int ch1_max)
    115. {
    116. rmax = ch1_max;
    117. }
    118.  
    119. void Dialog::on_ch2max_valueChanged(int ch2_max)
    120. {
    121. gmax = ch2_max;
    122. }
    123.  
    124. void Dialog::on_ch2maxspinBox_valueChanged(int ch2_max)
    125. {
    126. gmax = ch2_max;
    127. }
    128.  
    129. void Dialog::on_ch3max_valueChanged(int ch3_max)
    130. {
    131. bmax = ch3_max;
    132. }
    133.  
    134. void Dialog::on_ch3maxspinBox_valueChanged(int ch3_max)
    135. {
    136. bmax = ch3_max;
    137. }
    138.  
    139. ////////////////////////////////////////////////////////////////////////////////////////////////
    140. /// \brief Dialog::ShowCamera -> here we read frames and show them in label called camView
    141. /// temp is used only to make use of visual representation of image in Qt using QImage.
    142. ///
    143. ////////////////////////////////////////////////////////////////////////////////////////////////
    144. void Dialog::ShowCamera()
    145. {
    146. Camera.read(cameraFeed); ////////////////it should be cameraFeed otherwise.
    147. if(cameraFeed.empty() == true) return;
    148.  
    149. cv::inRange(cameraFeed,cv::Scalar(rmin,gmin,bmin),cv::Scalar(rmax,gmax,bmax),thresholdFeed);
    150.  
    151. morphOps(thresholdFeed);
    152. trackLP(x, y, thresholdFeed);
    153.  
    154.  
    155. ////////////////// visualization of image acquired from camera
    156.  
    157. cameraFeed.copyTo(temp);
    158. cv::cvtColor(temp, temp, CV_BGR2RGB);
    159. QImage qimgOriginal((uchar*) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
    160.  
    161.  
    162. ////////////////// visualization of binary image
    163.  
    164. QImage qimgProcessed((uchar*) thresholdFeed.data, thresholdFeed.cols, thresholdFeed.rows, thresholdFeed.step, QImage::Format_Indexed8);
    165.  
    166. ///////////////// show
    167.  
    168. ui->camView->setPixmap(QPixmap::fromImage(qimgOriginal));
    169. ui->outputView->setPixmap(QPixmap::fromImage(qimgProcessed));
    170.  
    171. }
    172.  
    173.  
    174. //////////////////////////////////////////////////////////////////////////////////////
    175. /// \brief Dialog::morphOps -> morphological operations
    176. /// \param thresh -> passed image from inRange to this
    177. ///////////////////////////////////////////////////////////////////////////////////////
    178. void Dialog::morphOps(cv::Mat &threshMorph)
    179. {
    180. ///create structuring element that will be used to "dilate" and "erode" image.
    181. ///the element chosen here is a 3px by 3px rectangle
    182. cv::Mat erodeElement = getStructuringElement( cv::MORPH_RECT, cv::Size(3,3));
    183. ///dilate with larger element so make sure object is nicely visible
    184. cv::Mat dilateElement = getStructuringElement( cv::MORPH_RECT,cv::Size(8,8));
    185.  
    186. cv::erode(threshMorph,threshMorph,erodeElement);
    187. cv::erode(threshMorph,threshMorph,erodeElement);
    188.  
    189. cv::dilate(threshMorph,threshMorph,dilateElement);
    190. cv::dilate(threshMorph,threshMorph,dilateElement);
    191.  
    192. }
    193.  
    194. /////////////////////////////////////////////////////////////////////////////////////////////
    195. /// \brief trackLP -> this tracks the x,y coordinate from binary image
    196. /// \param x -> x coordinate
    197. /// \param y -> y coordinate
    198. /// \param threshold -> binary image
    199. /// \param cameraFeed -> acquired image from camera
    200. //////////////////////////////////////////////////////////////////////////////////////////////
    201. void Dialog::trackLP(int &x, int &y, cv::Mat &threshTrack) //////// cv::Mat &cameraFeed
    202. {
    203. threshTrack.copyTo(temp1);
    204. cv::vector< cv::vector<cv::Point> > contours;
    205. cv::vector<cv::Vec4i> hierarchy;
    206. //find contours of filtered image using openCV findContours function
    207. findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );
    208. double refArea = 0;
    209. bool objectFound = false;
    210.  
    211. if (hierarchy.size() > 0)
    212. {
    213. int numObjects = hierarchy.size();
    214. //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
    215. if(numObjects<MAX_NUM_OBJECTS)
    216. {
    217. for (int index = 0; index >= 0; index = hierarchy[index][0])
    218. {
    219. cv::Moments moment = cv::moments((cv::Mat)contours[index]);
    220. double area = moment.m00;
    221.  
    222. //if the area is less than 5 px by 5 px then it is probably just noise
    223. //if the area is the same as the 3/2 of the image size, probably just a bad filter
    224. //we only want the object with the largest area so we safe a reference area each
    225. //iteration and compare it to the area in the next iteration.
    226. if(area>MIN_OBJ_AREA && area<MAX_OBJ_AREA && area>refArea)
    227. {
    228. x = moment.m10/area;
    229. y = moment.m01/area;
    230. objectFound = true;
    231. refArea = area;
    232. }
    233. else objectFound = false;
    234. }
    235. //let user know you found an object
    236. if(objectFound ==true)
    237. {
    238. ui->plainTextEdit->appendPlainText(QString("laser position: x=") + QString::number(x) + QString(" y=") + QString::number(y));
    239.  
    240. }
    241. }
    242. else
    243. {
    244. ui->plainTextEdit->appendPlainText(QString("still too much noise!"));
    245. }
    246. }
    247.  
    248. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. change part of a slider color in video cutter tool
    By DURGAPRASAD NEELAM in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2014, 06:51
  2. Dynamic label to right of slider resizes slider
    By zenzero-2001 in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2013, 10:11
  3. Event filtering
    By Cruz in forum Qt Programming
    Replies: 6
    Last Post: 28th March 2011, 12:35
  4. Key filtering
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2010, 09:10
  5. Replies: 2
    Last Post: 21st March 2010, 09:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.