[RESOLVED] How to loop inside a Picture which is captured from my webcam?
Hi
I am currently having problems on how to loop inside a picture which is being captured by a webcam.
it is already "bitmap" in form when i copied to from the clipboard to the picturebox?
i already figured out how to capture images from my webcam, but my problem now is to get the color inside the image being captured (the image is inside a picturebox)
i tried hard figuring this out. i have found alot of codes like: color under mouse, and color picker. but are they related to what i am making now?
sorry for my bad english.
I hope you guys can help me with this.
Here is my code:
Quote:
List1.Clear
px = Picture4.Width / Screen.TwipsPerPixelX / 2
py = Picture4.Height / Screen.TwipsPerPixelY / 2
For i = 1 To Picture4.Width / Screen.TwipsPerPixelX Step 3
For j = 1 To Picture4.Height / Screen.TwipsPerPixelY Step 3
'Convert to RGB
'Picture4.Circle (px * Screen.TwipsPerPixelX, py * Screen.TwipsPerPixelY), 10, RGB(255, 0, 0)
r = (GetPixel(Picture4.hdc, i, j) Mod 256)
b = Int(GetPixel(Picture4.hdc, i, j) / 65536)
g = (GetPixel(Picture4.hdc, i, j) - (b * 65536) - r) / 256
List1.AddItem "RGB=" & r & " " & g & " " & b
Next
Next
End Sub
is this the right way to get the image's color in rgb???
Re: How to loop inside a Picture which is captured from my webcam?
Welcome to the forums
Changing your picturebox scalemode to pixels during the loop can make things a bit easier
Code:
...
' add another variable for color so you don't have to use GetPixel 3 times
Dim lColor As Long
Picture4.ScaleMode = vbPixels ' change it back to twips (or whatever scalemode you are using) later
For i = 0 to Picture4.ScaleWidth - 1 ' << use inside width (ScaleWidth)
For j = 0 To Picture4.ScaleHeight - 1 ' << inside height
lColor = GetPixel(Picture4.hDC, i, j)
r = (lColor And &HFF&)
g = (lColor And &HFF00&) \ &H100
b = (lColor And &HFF0000) \ &H10000
List1.AddItem "RGB = " & r & " " & g & " " & b ' listing can be quite large!
Next
Next
Picture4.ScaleMode = vbTwips ' reset back to original scalemode
I used Hex values but you can use decimal if you prefer. To get decimal value from hex: Val(hexValue)
There are even faster ways to get the color data if interested. Search forum for GetDIBits API usage
Re: How to loop inside a Picture which is captured from my webcam?
hello, sir!
sorry for the late reply.
hmmm,
why do we have to minus 1 from the scalewidth and scaleheight sir?
is it wrong to just loop it from 1 to picture4.scalewidth (or to picture4.scalewidth)?
thank you so much.
Re: How to loop inside a Picture which is captured from my webcam?
UPDATE!!!
I've tested your code sir and it works just fine with mine here is the code
vb Code:
Private Sub Command1_Click()
Dim px As Long
Dim py As Long
Dim point As Long
Dim r As Long
Dim g As Long
Dim b As Long
Picture4.ScaleMode = vbPixels
List1.Clear
Picture4.DrawWidth = 1
px = Picture4.ScaleWidth / Screen.TwipsPerPixelX / 2
py = Picture4.ScaleHeight / Screen.TwipsPerPixelY / 2
For i = 0 To Picture4.ScaleWidth - 1
For j = 0 To Picture4.ScaleHeight - 1
'Convert to RGB
r = (GetPixel(Picture4.hdc, i, j) Mod 256)
b = Int(GetPixel(Picture4.hdc, i, j) / 65536)
g = (GetPixel(Picture4.hdc, i, j) - (b * 65536) - r) / 256
If r > 250 And r < 255 Then
Picture4.Circle (i, j), 2, RGB(0, 255, 0)
List1.AddItem "RGB=" & r & " " & g & " " & b
End If
Next
Next
Picture4.ScaleMode = vbTwips ' reset to original scale mode
End Sub
Re: How to loop inside a Picture which is captured from my webcam?
Quote:
Originally Posted by
skemb321
hello, sir!
sorry for the late reply.
hmmm,
why do we have to minus 1 from the scalewidth and scaleheight sir?
is it wrong to just loop it from 1 to picture4.scalewidth (or to picture4.scalewidth)?
thank you so much.
The reason is simple. The scalewidth & scaleheight starts at 0 not 1, so adjust by using -1.
And regarding your newest code, it would be quite a bit faster if you used the logic I posted. Speed will be increased by not calling GetPixel 3 times for every pixel. Also using integer division (\) is faster than 'real' division (/)
Re: How to loop inside a Picture which is captured from my webcam?
It always starts at 0,0. A Width of 4 would be 0,1,2,3. This is not without good reason as it simplifies the internal maths involved so it's worth getting used to.
Re: How to loop inside a Picture which is captured from my webcam?
Quote:
Originally Posted by
Milk
It always starts at 0,0. A Width of 4 would be 0,1,2,3. This is not without good reason as it simplifies the internal maths involved so it's worth getting used to.
:bigyello: tnx!
finally, got the idea now.