Comment modifier la valeur d'un pixel en C++ sous Visual C++ embarqué
Bonjour
Je débute dans ce domaine.
J'ai actuellement le code suivant, qui compare deux images et renvoie vrai ou faux si elles sont égales ou differentes.
j'utilise la fonction LockBit pour accéder à la mémoire. Le problème est que j'ai j'y accède en lecture. Je voudrais y accéder en écriture en meme temps pour modifier la valeur des pixels,mais je n'y arrive pas. Auriez-vous des conseils? Peut-être devrais-je revoir le problème?
Le but est de pouvoir modifier les pixels de pBitmapImageOld.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| BOOL IsMotionDetected4 (IBitmapImage* pBitmapImage, IBitmapImage *pBitmapImageOld,PBMPFMT pbmpf, DWORD dwSenstivity,int color)
{
int k = 0;
int red = 0;
int green = 0;
int blue = 0;
int iNoMotionDetectedThreshold = 0;
long MoreThreshCnt=0; // reset the thresh counter
int TreshPerix = 18;
float TreshPerixUnit = ((float)50/24);
PCHAR lpByte1 = NULL;
PCHAR lpByte2 = NULL;
int size1 = NULL;
int size2 = NULL;
int size = NULL;
int bytesPerPixel = 3; // for 24 bits
USHORT pixel = 0;
HRESULT hr = S_OK;
double dSenstivityFactor = 0.006;
RECT rect = {0};
rect.right = pbmpf->nWidth;
rect.bottom = pbmpf->nHeight;
BitmapData lockedBmpData1;
lockedBmpData1.Reserved = 0;
hr = pBitmapImageOld->LockBits(&rect, ImageLockModeRead, pbmpf->PixelFormat, &lockedBmpData1);
BitmapData lockedBmpData2;
lockedBmpData2.Reserved = 0;
hr = pBitmapImage->LockBits(&rect, ImageLockModeRead, pbmpf->PixelFormat, &lockedBmpData2);
///////////////////////////////
if (S_OK != hr)
{
lockedBmpData2 = lockedBmpData1;
}
double grey1 = 0, grey2 = 0;
for (int y=10; y<pbmpf->nHeight-20; y=y+1)
{
for (int x=10; x<pbmpf->nWidth-10; x=x+2)
{
BYTE* pPixel = (BYTE*)lockedBmpData1.Scan0+(y*lockedBmpData1.Stride)+(x*3);
blue = *pPixel;
green= *(pPixel+1);
red = *(pPixel+2);
grey1 = ceil(0.212671 * red + 0.715160 * green + 0.072169 * blue);
pPixel = (BYTE*)lockedBmpData2.Scan0+(y*lockedBmpData2.Stride)+(x*3);
blue = *pPixel;
green= *(pPixel+1);
red = *(pPixel+2);
grey2 = ceil(0.212671 * red + 0.715160 * green + 0.072169 * blue);
if (abs((int)(grey1-grey2)) > (int)(TreshPerix))
{
// if diff between this pix in prev and cur frame > TreshPerix
MoreThreshCnt++; // increase motion pixels counters
Quand le test est vrai, Je voudrais ici pouvoir remplacer le pixel rouge par 255
}
}
}
hr = pBitmapImageOld->UnlockBits(&lockedBmpData1);
hr = pBitmapImage->UnlockBits(&lockedBmpData2);
// Set iNoMotionDetectedThreshold to number of pixels that must change
// in the two frames before motion is said to be detected.
// g_iSenstivity is set by the user,
iNoMotionDetectedThreshold = (int)(dSenstivityFactor*dwSenstivity * pbmpf->nWidth * pbmpf->nHeight);
//////////////////////////////////////////////////////////
// See if motion detected
if (MoreThreshCnt > iNoMotionDetectedThreshold)
{
return TRUE;
}
// No motion detected
return FALSE;
} |
Je vous remercie de votre aide.
A bientot
zouzou32300