Selected area in transforms

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
JesusRL
Posts: 70
Joined: Wed Aug 05, 2020 4:36 pm
Location: Madrid

Selected area in transforms

#1

Post by JesusRL »

Hi Robin,

I would like to make some statistics in the selected area used by a focusing transform.

Do you have an already accesible buffer including only the selected area that could be used?

Or, should I access the global image buffer and extract the valid values according to SharpCap.Transforms.SelectionRect?

T&R
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Selected area in transforms

#2

Post by admin »

Hi,

you should be able to call CutROI(Rectangle) on the full frame to cut out the part that corresponds to the selection rectangle, and then for many processing on that. Two things to note:

1) don't forget to release the cutout copy otherwise you will run out of memory rapidly

Code: Select all

clipped = fullFrame.CutROI(rectangle)
# image processing
clipped.Release()
2) you are working with raw colour images and the top or left position of the rectangle are odd numbers then the Bayer pattern of the cutout area will be different to that are the full frame. SharpCap takes account of this and sets the properties correctly on the newly cut out frame, but you may need to be aware of this

cheers,

Robin
JesusRL
Posts: 70
Joined: Wed Aug 05, 2020 4:36 pm
Location: Madrid

Re: Selected area in transforms

#3

Post by JesusRL »

Thank you Robin,

I will explain you what I will do to contextualize the questions I make:

I normally focus wth single FWHM; I consider (may I be wrong?) the best scenario would be to use a star with max value pixels from 50% to 80% of the max value (in my case 16b >> 65535)

My intention is to have, while I am using the focus transform, a permanent (updated per frame) Notification with the max value, and colored according to situation: yellow if below, green on range and red if above (I have seen these 3 colours in the application notifications I receive)

Now my questions:

1) I have a more restless than wise mind; it may be a basic thing but I am an amateur programmer and have no idea on how to extract the single values of each pixel in the frame (or subframe) in order to use the statistically (or any other use). This is what I have to the point, cannot advance

2) I guess the colour of the Notification is defined via the NotificationStatus (normally 0) but I dont't know how to change this value (no int or str is accepted)

Code: Select all

#I have made a mix of different pieces of code from the forum to get frame, clipped, lease, framedata_addr, but after many attempts cannot get the values anyway

def handler( sender, eventArgs):
	frame          = eventArgs.Frame
	rectangle      = SharpCap.Transforms.SelectionRect
	clipped        = frame.CutROI(rectangle)
	lease          = clipped.GetBufferLease()
	info           = clipped.Info
	www 	       = info.Width
	hhh            = info.Height
	frameData_addr = lease.Buffer.ToInt64()
	actual_max     = 0
	global_max     = 65535
	
	for x in range(www * hhh):
#QUESTION 1###########################################################################
		value = extract someway the value from the buffer [x]
############################################################################
		if value > actual_max: actual_max = value
		
	lease.Dispose()	
	clipped.Release()	
#QUESTION 2###########################################################################
	SharpCap.ShowNotification('Image max value = ' +  actual_max, NotificationStatus that defines the colour, False, 1)
############################################################################
T&R
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Selected area in transforms

#4

Post by admin »

Hi,

ok, so you can extract the frame data like this

Code: Select all

...
clipped        = frame.CutROI(rectangle)

data = clipped.ToBytes() # for 8 bit data, returns a 1 dimensional array of w*h*colourplanes bytes, index as plane + x * colourplanes + y * width * colourplanes
...or...
data = clipped.ToUshorts() # for 16 bit data, returns a 1 dimensional array of w*h*colourplanes unsigned shorts, index as plane + x * colourplanes + y * width * colourplanes
...or...
data = clipped.ToFloats(0) # for either, returns a 2 dimensional array of 32 bit floats. The parameter is actually ignored, you get the first colour plane if more than one

As to the notifications, try this

Code: Select all

from SharpCap.Base import NotificationStatus
SharpCap.ShowNotification("hello", NotificationStatus.Error)
You can see more about the parameters by allowing the help popup to appear when you start to type a method name - like this
Capture.PNG
Capture.PNG (11.67 KiB) Viewed 2147 times
Unfortunately all the overloads appear on one line - I'll see if I can fix that.

Robin
JesusRL
Posts: 70
Joined: Wed Aug 05, 2020 4:36 pm
Location: Madrid

Re: Selected area in transforms

#5

Post by JesusRL »

Hi Robin,

First of all thank you for your patience :)

Almost every thing working ... but I wanted to check coherence of my data and there is something I do not understand ...

I am using the frame from FrameCaptured() so it should be RAW (I guess, please correct me)

I am using the method .ToFloat() (actually if I try .ToUshort() I get an error 'BufferFrame' object has no attribute 'ToUshort') as being it a RAW frame should give me all values as there are no colorplanes yet (again correct me if wrong)

To check the coherence I have calculated the mean, and compared to yours and it gives me slightly different values; what I don't understand

Could you please check this short code (in case my previous thoughts were correct, of course)

Code: Select all

from SharpCap.Base import NotificationStatus

def handler( sender, eventArgs):
	frame          = eventArgs.Frame
	rectangle      = SharpCap.Transforms.SelectionRect
	clipped        = frame.CutROI(rectangle)
	data 		   = clipped.ToFloats(0)
	info           = clipped.Info
	www 		   = info.Width
	hhh            = info.Height
	
	global_max     = 65535
	min_OK         = 0.5
	max_OK         = 0.8
	actual_max     = 0
	total_sum      = 0
	
	for x in range(hhh):
		for y in range(www):
			value = data[x,y]
			if value > actual_max: actual_max = value
			total_sum += value
			
	average = total_sum / (www * hhh)

	if actual_max < (global_max * min_OK):
		SharpCap.ShowNotification('Image max value below min = ' + str(actual_max) + ' / ' + str(global_max * min_OK), NotificationStatus.Warning)
	else:
		if actual_max > (global_max * max_OK):
			SharpCap.ShowNotification('Image max value above max = ' +  str(actual_max) + ' / ' + str(global_max * max_OK), NotificationStatus.Error)
		else:
			SharpCap.ShowNotification('Image max value = ' +  str(actual_max))
	#VALIDITY CHECK
	print average
	print clipped.GetStats()
	
	clipped.Release()	

SharpCap.SelectedCamera.FrameCaptured += handler
T&R
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Selected area in transforms

#6

Post by admin »

Hi,

a few things...

* to get the integer 16 bit values, the method is ToUshorts() with an s at the end, not ToUshort() - could that be the problem?

* the differences could be one or both of the following

1) Rounding errors - by setting total_sum=0 initially, it is an integer, which becomes a float. Try total_sum=0.0
2) Only measuring the first colour plane (blue) if you are using a colour camera. This is the limitation in ToFloats() that only returns the first plane regardless of the parameter. SharpCap's measurement will include all colours.

Robin
JesusRL
Posts: 70
Joined: Wed Aug 05, 2020 4:36 pm
Location: Madrid

Re: Selected area in transforms

#7

Post by JesusRL »

Hi Robin,

Sorry I refered to the single expresion ToUshort(), but I was actually trying ToUshorts() ... It doesnt work :(

Image
http://nto.org.es/SHARED_GLOBAL/Capture.JPG
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Selected area in transforms

#8

Post by admin »

Ooops, sorry. ToUshorts() is new in SharpCap 3.3 - it's not there in 3.2

Robin
JesusRL
Posts: 70
Joined: Wed Aug 05, 2020 4:36 pm
Location: Madrid

Re: Selected area in transforms

#9

Post by JesusRL »

Another reason to be anxious.

Thank you Robin
Post Reply