How i would approximate a c &pointer call in cffi/lisp

Discussion of Common Lisp

How i would approximate a c &pointer call in cffi/lisp

Postby joeish80829 » Sat Sep 21, 2013 1:06 am

if you got a sec to tell me off the top of your head how i would approximate a c &pointer call in lisp I would much appreciate it.

Im trying to mimic the below code in lisp:

Code: Select all
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>

int main()
{
         int bright=50;

         cvNamedWindow("MyWindow");

         //create trackbars
         cvCreateTrackbar("brightness", "MyWindow", &bright, 100, NULL);

         //load the original image
         IplImage* img = cvLoadImage("C:/MyPic.jpg");

           //create a blank image
         IplImage* des = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);

         while(1){


             //change the brightness of the image
             cvAddS(img, cvScalar(bright-50,bright-50,bright-50), des);


             cvShowImage("MyWindow", des);

             int c=cvWaitKey(40);
             if(c==27){       
                 //cleaning up
                 cvDestroyWindow("MyWindow");
                 cvReleaseImage(&img);
                 cvReleaseImage(&des);
                 break;
             }
         }

         return 0;
}


and in this part of the above code:

cvCreateTrackbar("brightness", "MyWindow", &bright, 100, NULL);

is the "&bright" part .....I'm sure you can see what the &bright is supposed to ....its supposed to change the bright variable when the trackbar is moved and in my code a trackbar actually shows up in window but brightness of the image doesn't change as its supposed to when trackbar is moved.

i just could use help approximating the correct c opencv behavior in my lisp code.

here is my code so far...i know you can't run it but hopefully looking at it you can give me ideas...

Code: Select all
(defun create-trackbar-example (filename)
  "Open the image FILENAME and show it in a window."
  (let* ((image (load-image filename 1))
         (img-size (get-size image))
        (dest (create-image img-size +ipl-depth-8u+ 3))
        (window-name "CREATE-TRACKBAR Example"))
        (named-window window-name +window-normal+)
    (do* ((bright (cffi:foreign-alloc :int :initial-element 50))
           (scalar (make-cv-scalar (- (cffi:mem-ref bright :int) 50)
                                   (- (cffi:mem-ref bright :int) 50)
                                   (- (cffi:mem-ref bright :int) 50))))
         ((plusp (wait-key *millis-per-frame*)) nil)
         (create-trackbar "brightness" window-name bright 100 (cffi:null-pointer))
     (adds image scalar dest)
     (move-window window-name 650 350)
     (show-image window-name image))
     (release-image image)
     (destroy-window window-name)))


here is the working create-trackbar wrapper if that helps:

Code: Select all
;; int cvCreateTrackbar(const char* trackbar_name, const char* window_name, int* value, int count, CvTrackbarCallback
;; on_change=NULL )
(cffi:defcfun ("cvCreateTrackbar" %create-trackbar) :int
  "Creates a trackbar and attaches it to the specified window."
  (trackbar-name :string)
  (window-name :string)
  (value :pointer)
  (count :int)
  (on-change cv-trackbar-callback))

(defun create-trackbar (trackbar-name window-name value count &optional (on-change (cffi:null-pointer)))
  (%create-trackbar trackbar-name window-name value count on-change))


thanks again ....I do appreciate all the help the S.O. community gives me..

cheerio =)
Last edited by nuntius on Mon Sep 23, 2013 10:39 am, edited 1 time in total.
Reason: added [code] block
joeish80829
 
Posts: 151
Joined: Tue Sep 03, 2013 5:32 am

Re: How i would approximate a c &pointer call in cffi/lisp

Postby Goheeca » Sun Sep 22, 2013 7:20 am

Create a foreign object:
Code: Select all
(defvar *bright* (cffi:foreign-alloc :int))

and used it:
Code: Select all
(create-trackbar "brightness" window-name *bright* 100 (cffi:null-pointer))

and access it:
Code: Select all
(cffi:mem-ref *bright* :int)

and, of course, don't forget free the object:
Code: Select all
(cffi:foreign-free *bright*)
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 197
Joined: Thu May 10, 2012 12:54 pm

Re: How i would approximate a c &pointer call in cffi/lisp

Postby joeish80829 » Tue Oct 01, 2013 6:20 am

Thanks for your help on this with your help I made it work....now i was hoping since your so good at this maybe you can help me convert this other function here http://opencv-srf.blogspot.com/2011/11/track-bars.html uder the Track Bars with Callback Function heading halfway down the page

it uses the last parameter of cvCreateTrackbar here:
http://docs.opencv.org/modules/highgui/ ... tetrackbar


I've tried many different ways and below is my latest attempt...but i could really use your help on this one ....I just have to link the change-contrast funtion to the create-trackbar funtion but i would need to know the correct cffi functions to use i think it might be these http://common-lisp.net/project/cffi/man ... #Callbacks but could use more guidance

(defun change-contrast (contrast window-name img dest)
(if (< contrast 10) (scale img dest (- 11 contrast))
(scale img dest (- contrast 9)))
(show-image window-name dest))



(defun create-trackbar-example (&optional (camera-index 0) (width *default-width*)
(height *default-height*))
"Creates a trackbar and attaches it to the specified window.
Move the slider to adjust the brightess of the camera output"
(with-capture (capture (create-camera-capture camera-index))
(let ((window-name "CREATE-TRACKBAR Example")
(contrast (cffi:foreign-alloc :int :initial-contents '(50))))
(set-capture-property capture +cap-prop-frame-width+ width)
(set-capture-property capture +cap-prop-frame-height+ height)
(named-window window-name)
(move-window window-name 600 175)
(do* ((frame (query-frame capture) (query-frame capture))
(dest (clone-image frame))
(scalar 0))
((plusp (wait-key *millis-per-frame*)) nil)
(format t "Trackbar level: ~a~%~%" (cffi:mem-ref contrast :int))
(create-trackbar "Brightness" window-name contrast 100 (change-contrast contrast window-name frame dest))
(setf scalar (scalar (- (cffi:mem-ref contrast :int 50))
(- (cffi:mem-ref contrast :int) 50)
(- (cffi:mem-ref contrast :int) 50)))
(add-s frame scalar dest)
(show-image window-name dest))
(destroy-window window-name))))



for reference here is the function that works that you helped me build....thanks again for that


(defun create-trackbar-example (&optional (camera-index 0) (width *default-width*)
(height *default-height*))
"Creates a trackbar and attaches it to the specified window.
Move the slider to adjust the brightess of the camera output"
(with-capture (capture (create-camera-capture camera-index))
(let ((window-name "CREATE-TRACKBAR Example")
(bright (cffi:foreign-alloc :int :initial-contents '(50))))
(set-capture-property capture +cap-prop-frame-width+ width)
(set-capture-property capture +cap-prop-frame-height+ height)
(named-window window-name)
(move-window window-name 600 175)
(do* ((frame (query-frame capture) (query-frame capture))
(dest (clone-image frame))
(scalar 0))
((plusp (wait-key *millis-per-frame*)) nil)
(format t "Trackbar level: ~a~%~%" (cffi:mem-ref bright :int))
(create-trackbar "Brightness" window-name bright 100)
(setf scalar (scalar (- (cffi:mem-ref bright :int 50))
(- (cffi:mem-ref bright :int) 50)
(- (cffi:mem-ref bright :int) 50)))
(add-s frame scalar dest)
(show-image window-name dest))
(destroy-window window-name))))
joeish80829
 
Posts: 151
Joined: Tue Sep 03, 2013 5:32 am


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 3 guests

cron