Mouse coordinates - c

How can i get mouse coordinates in C under Mac?

I'm not avare of any fully only C implementation, but in the foundation framework of OSX (10.5+) there is a function called "HIGetMousePosition". You should be able to integrate this with your C program.
http://allancraig.net/index.php?option=com_content&view=article&id=137:getting-mouse-coordinates&catid=39:objective-c&Itemid=86 shows this example implementation:
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
HIPoint point;
HICoordinateSpace space = 2;
HIGetMousePosition(space, NULL, &point);
printf("%.2f %.2f", point.x, point.y);
[pool drain];
return 0;
}

Related

is there a way to convert type char to structTexture in Raylib

I keep getting the error "func.h:23: error: cannot cast 'int' to 'struct Texture'" even though im inputting text as tile in renderTiles(). Am i just being dumb here? ¯_(ツ)_/¯ Im new to C so this might just be me. i copied and pasted the basic window example to create this. i know there isnt char before tile in "renderTiles(tile)" i tried that as well and it still did not work
main.c:
*
* raylib [core] example - Basic window
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* Example originally created with raylib 1.0, last time updated with raylib 1.0
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2013-2022 Ramon Santamaria (#raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "func.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 768;
const int screenHeight = 576;
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// textures
Texture2D grass = LoadTexture("grass.png");
Texture2D stone = LoadTexture("stone.png");
Texture2D sand = LoadTexture("sand.png");
Texture2D stone_oasis = LoadTexture("stone-oasis.png");
Texture2D sand_oasis = LoadTexture("sand-oasis.png");
Texture2D UI = LoadTexture("ui.png");
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTexture(UI, 0, 0, RAYWHITE);
renderTiles("grass");
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}
func.h
#ifndef FUNC
#define FUNC
const int screenWidth = 768;
const int screenHeight = 576;
int centerX(int x)
{
int ox = x + (screenWidth / 2);
return (ox);
}
int centerY(int y)
{
int oy = y + (screenHeight / 2);
return(oy);
}
void renderTiles(tile)
{
for( int b = 0; b < 12; b = b + 1)
{
for( int a = 0; a < 12; a = a + 1 ){
DrawTexture(tile, (a * 48) + 96, (b * 48), RAYWHITE);
}
}
}
#endif
Answering the question title, you have already converted the string to a texture struct with
Texture2D grass = LoadTexture("grass.png");
The raylib cheat sheet shows the function you are calling to be
void DrawTexture(Texture2D texture, int posX, int posY, Color tint);
but you have passed the char* value "grass" to your function, which has an untyped argument, so the compiler assumes it to be type int.
Your function should be
/* void renderTiles(tile) */
void renderTiles(Texture2D tile)
and you should call it with
/* renderTiles("grass"); */
renderTiles(grass);
Also, you should not have executable code in a header file.

How to pass Parameters?

I have been converting convert rose.png -sparse-color barycentric '0,0 black 69,0 white roseModified.png into MagickWand C API.
double arguments[6];
arguments[0] = 0.0;
arguments[1] = 0.0;
// arguments[2] = "black";
arguments[2] = 69.0;
arguments[3] = 0.0;
// arguments[5] = "white";
MagickSparseColorImage(wand0, BarycentricColorInterpolate, 4,arguments);
MagickWriteImage(wand0,"rose_cylinder_22.png");
I don't know how to pass the double argument. click here
for method's defenition.
UPDATE:
Source Image
After I executed convert rose.png -sparse-color barycentric '0,0 black 69,0 white' roseModified.png, I got below Image
I haven't got the output like this with my C program. There might be something with white and black.
For sparse colors, you need to convert the color to doubles for each channel. Depending on how dynamic you need to generate spares color points, you may need to start building basic stack-management methods.
Here's an example. (Mind that this is a quick example, and can be improved on greatly)
#include <stdlib.h>
#include <MagickWand/MagickWand.h>
// Let's create a structure to keep track of arguments.
struct arguments {
size_t count;
double * values;
};
// Set-up structure, and allocate enough memory for all colors.
void allocate_arguments(struct arguments * stack, size_t size)
{
stack->count = 0;
// (2 coords + 3 color channel) * number of colors
stack->values = malloc(sizeof(double) * (size * 5));
}
// Append a double value to structure.
void push_double(struct arguments * stack, double value)
{
stack->values[stack->count++] = value;
}
// Append all parts of a color to structure.
void push_color(struct arguments * stack, PixelWand * color)
{
push_double(stack, PixelGetRed(color));
push_double(stack, PixelGetGreen(color));
push_double(stack, PixelGetBlue(color));
}
#define NUMBER_OF_COLORS 2
int main(int argc, const char * argv[]) {
MagickWandGenesis();
MagickWand * wand;
PixelWand ** colors;
struct arguments A;
allocate_arguments(&A, NUMBER_OF_COLORS);
colors = NewPixelWands(NUMBER_OF_COLORS);
PixelSetColor(colors[0], "black");
PixelSetColor(colors[1], "white");
// 0,0 black
push_double(&A, 0);
push_double(&A, 0);
push_color(&A, colors[0]);
// 69,0 white
push_double(&A, 69);
push_double(&A, 0);
push_color(&A, colors[1]);
// convert rose:
wand = NewMagickWand();
MagickReadImage(wand, "rose:");
// -sparse-color barycentric '0,0 black 69,0 white'
MagickSparseColorImage(wand, BarycentricColorInterpolate, A.count, A.values);
MagickWriteImage(wand, "/tmp/output.png");
MagickWandTerminus();
return 0;
}

Serial Number Detection with openCV

I have a video with a serial number. Like in the picture. How I can with openCV detect the position of this patron. The only thing that I need is detect the location of this patron. Always this patron will have 12 numbers and will be white.
example
using Morphological Transformations you can find location of numbers.
try the code below (not a perfect code, it is just for instruction)
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src=imread("dnpaP.jpg");
Mat thresh = src.clone();
dilate(thresh,thresh,Mat(),Point(-1,-1), 5);
erode(thresh,thresh,Mat(),Point(-1,-1), 5);
cvtColor(thresh, thresh, COLOR_BGR2GRAY);
threshold(thresh, thresh, 200, 255, THRESH_BINARY);
erode(thresh,thresh,Mat(),Point(-1,-1), 3);
dilate(thresh,thresh,Mat(),Point(-1,-1), 3);
vector<vector<Point> > contours;
findContours(thresh.clone(), contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
for( size_t i = 0; i< contours.size(); i++ )
{
Rect boundingRect_ = boundingRect( contours[i] );
if(boundingRect_.width > boundingRect_.height * 12)
rectangle(src,boundingRect_,Scalar(0,0,255),2);
}
imshow("thresh",thresh);
imshow("src",src);
waitKey();
}

C decorator design pattern (using the pizza example)

I are taking a low level course in embedded systems and have been assigned the task of replicating some design patterns in C. I have gotten observer and delegator working I am really struggling with the decorator pattern. I do realize that many people think that design patterns do not belong in a "low-level" language like C but I do not have an option - it needs to be done to pass this course. All the examples I have found are for OO programming languages. I'm using this Java pizza example as a basis (just returning the cost to make it easy) but for the life of me cannot get it to work: http://www.newthinktank.com/2012/09/decorator-design-pattern-tutorial/
This is the UML for the example (as I said I am only doing the getCost part):
I have spent about 2 days trying to get this to work but am just stuck. I have added the code that I have but am stumped how to add the tomato to the pizza so that the cost is added up correctly
#include <stdio.h>
typedef struct _pizza {
double (* getCost) ();
} pizza_t;
typedef struct _toppingDecorator {
double (* getCost) ();
pizza_t tempPizza;
} toppingDecorator_t;
// these are the pizzas
double plainPizzaCost () {
return 5;
}
double thickCrustPizzaCost () {
return 7;
}
// these are the toppings
double mozzarellaCost (toppingDecorator_t * self) {
return self->tempPizza.getCost () + 3.0;
}
double tomatoCost (toppingDecorator_t * self) {
return self->tempPizza.getCost () + 1;
}
int main(int argc, const char * argv[]) {
pizza_t plainPizza;
plainPizza.getCost = &plainPizzaCost;
pizza_t thickCrustPizza;
thickCrustPizza.getCost = &thickCrustPizzaCost;
toppingDecorator_t mozzarella;
mozzarella.tempPizza = plainPizza;
mozzarella.getCost = &mozzarellaCost;
toppingDecorator_t tomato;
tomato.tempPizza = mozzarella.tempPizza;
tomato.getCost = &tomatoCost;
// now print the cost
printf ("A plain pizza costs %f\n", plainPizza.getCost ());
printf ("A mozzarella pizza costs %f\n", mozzarella.getCost (&mozzarella));
printf ("A tomato and mozzarella pizza costs %f\n", tomato.getCost (&mozzarella));
}
not sure why the post was down voted but anyway... A friend solved the problem for me and I thought I'd post the answer here - thank you Marcel :o)
#include <stdio.h>
#include <stdlib.h>
typedef struct _pizza pizza_t;
typedef double (* getCost)(struct _pizza * self);
typedef struct _pizza {
getCost getCostFunc;
} pizza_t;
typedef struct _plainPizza {
pizza_t base;
} plainPizza_t;
typedef struct _toppingDecorator {
pizza_t base;
pizza_t * decorate;
} toppingDecorator_t;
// these are the pizzas
double plainPizzaCost (plainPizza_t self) {
return 5;
}
// these are the toppings
double mozzarellaCost (toppingDecorator_t * self) {
return self->decorate->getCostFunc(self->decorate) + 3;
}
double tomatoCost (toppingDecorator_t * self) {
return self->decorate->getCostFunc(self->decorate) + 2;
}
double salamiCost (toppingDecorator_t * self) {
return self->decorate->getCostFunc(self->decorate) + 1;
}
int main(int argc, const char * argv[]) {
plainPizza_t plainPizza;
plainPizza.base.getCostFunc = (getCost) plainPizzaCost;
toppingDecorator_t mozzarella;
mozzarella.base.getCostFunc = (getCost) mozzarellaCost;
mozzarella.decorate = (pizza_t *) &plainPizza;
toppingDecorator_t tomato;
tomato.base.getCostFunc = (getCost) tomatoCost;
tomato.decorate = (pizza_t *) &mozzarella;
toppingDecorator_t salami;
salami.base.getCostFunc = (getCost) salamiCost;
salami.decorate = (pizza_t *) &tomato;
printf ("A tomato pizza costs %f\n", tomato.base.getCostFunc((pizza_t *) &tomato));
printf ("A salami pizza costs %f\n", salami.base.getCostFunc((pizza_t *) &salami));
}

Threading face_detection on camera with opencv

I am training to use thread on face,nose,eyes detection. Because, when I did not, the camera is working very slowly. I wrote this code. I can not find mistake in code. But when I compiled it is giving exception on taskCollection tab pChore->m_pFunction(pChore); error.
#include <iostream>
#include "cv.h"
#include "highgui.h"
#include <pthread.h>
#include <stdio.h>
struct parameter_t{
IplImage* capturedImg;
CvHaarClassifierCascade* pCascade_face;
CvMemStorage* storage;
};
void* threadface_func(void* parameter){
CvSeq * detectRect_face;
parameter_t *p =(parameter_t*)parameter;
detectRect_face=cvHaarDetectObjects(p->capturedImg,p->pCascade_face,p->storage,1.15, 3, 0,cvSize(50,50));
for(int i=0;i<(detectRect_face ? detectRect_face->total:0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem(detectRect_face, i);
CvPoint pt1 = { r->x, r->y };
CvPoint pt2 = { r->x + r->width, r->y + r->height };
cvRectangle(p->capturedImg, pt1, pt2, CV_RGB(255,0,0), 1,8, 0);
}
return 0;
}
int main ()
{
CvCapture* capture = cvCaptureFromCAM(0);
IplImage* capturedImg;
int resCount = 1;
int flags = CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH;
CvHaarClassifierCascade * pCascade_face;
pthread_t threadface;
pCascade_face = (CvHaarClassifierCascade *)cvLoad("C:/Users/Furkan/Desktop/Computer Vision/Programlar/opencv/data/haarcascades/haarcascade_frontalface_alt.xml");
cvNamedWindow("FaceDetection");
while (true)
{
CvMemStorage * storage = 0;
capturedImg = cvQueryFrame(capture);
storage = cvCreateMemStorage(0);
parameter_t my_parameters;
my_parameters.capturedImg=capturedImg;
my_parameters.storage=storage;
my_parameters.pCascade_face=pCascade_face;
int k=pthread_create(&threadface,0,threadface_func,(void*)&my_parameters);
if(k!=0)
{
printf("Create thread failed! error");
return 1;
}
cvShowImage("FaceDetection", capturedImg);
}
cvDestroyWindow("FaceDetection");
cvReleaseCapture(&capture);
pthread_exit(NULL);
return 0;
}
Please Help.
the IplImages you get from the capture are pointing to videodriver-memory. to use them in another thread, youve got to clone() them. (i see, that you're even trying to draw into that).
you're generating new threads at an absurd high rate there, without ever waiting for one to finish
i can't see any lock/mutex in your code
please reconsider using multiple threads at all. at least, it won't work like this
(seems, that your opencv version & your api use could need an upgrade, too .. )

Resources