CvPoint qiymatlarini sozlash

OpenCV da CvPoint tizimini foydalanayapman va tuzumning x va y maydonlariga qiymat tayinlashingiz kerak.

Mana mening kodim:

CvPoint* P1;
P2[0].x=32;

Ammo qiymatlarni belgilashda dastur doimo bloklanadi.

Ushbu qadriyatlarni qanday o'rnatish haqida har qanday fikr?

1
Savol C ++ tagiga kiritilgani uchun, siz har qanday holda cv :: Point ob'ektidan foydalanishingiz mumkin. Othwerwise, savolni Cga takrorlash yaxshi fikr bo'lishi mumkin.
qo'shib qo'ydi muallif Christian Rau, manba
P1 nomi ko'rsatgichi o'zgaruvchisi yaratdingiz va kursor o'zgaruvchisi P2 ga qiymatlarni belgilashga harakat qilyapsizmi? cvPoint P1 = cvPoint (32, 32); kabi biror narsa qilishingiz kerak
qo'shib qo'ydi muallif Alok Save, manba

2 javoblar

Well first of all P1 is a pointer to an object of type P1. In order to assign something to an object's member via its pointer you need to use the -> operator. If this pointer points to the beginning of an array you use the operator[] to access individual elements. This operator returns a reference for the given index, in this case CvPoint& .

1. bitta ob'ektni dinamik taqsimlash

CvPoint* P1 = new CvPoint();//default construction of an object of type CvPoint
P1->x = 32;

// do something with P1

// clean up 
delete P1;

2. dinamik ajratish yoki bir qator

CvPoint* points = new CvPoint[2];//array of two CvPoints
points[0].x = 32;//operator[] returns a reference to the CvPoint at the given index
points[1].x = 32;

// do something with points

// clean up
delete[] points;

Ikkala misolda ham yangi operator ishlatilganligi sababli, ularni o'chirish yoki bir qatorda o'chirish [] uchun mos keladigan qo'ng'iroq bilan birlashtirish majburiydir.

4
qo'shib qo'ydi

Dinamik usul:

CvPoint P1;

P1.x=32;

P1.y=32;

//////////////

CvPoint P2[2];

P2[0].x=32;

P2[0].y=32;
2
qo'shib qo'ydi