Or you try this small script ...
It is not finish. It has a big problem with the 16 bit MONO images (it simply not work ... yet).
So please, first set your camera to MONO8 (if you have a monochrome camera).
Code: Select all
# *******************************************************************************************
# SharpCap script "xxxxxxxxx.py"
# 2024/08/26 Jean-Francois
#
# Version: 1.0.0: First version
#
# The script works only with a SharpCap PRO version.
# *******************************************************************************************
import os
import math
import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
import System.Drawing
import System.Windows.Forms
from System import Array
from System.Drawing import *
from System.Windows.Forms import *
from System.Threading import Thread, ApartmentState, ParameterizedThreadStart
H = SharpCap.SelectedCamera.Controls.Resolution.CaptureArea.Height
W = SharpCap.SelectedCamera.Controls.Resolution.CaptureArea.Width
Boxsize = 100
bH = Boxsize
bW = Boxsize
Rect_1 = Rectangle(0, 0, bW, bH)
Rect_2 = Rectangle((W-bW)//2, 0, bW, bH)
Rect_3 = Rectangle(W - bW, 0, bW, bH)
Rect_4 = Rectangle(0, (H-bH)//2, bW, bH)
Rect_5 = Rectangle((W-bW)//2, (H-bH)//2, bW, bH)
Rect_6 = Rectangle(W - bW, (H-bH)//2, bW, bH)
Rect_7 = Rectangle(0, H - bH, bW, bH)
Rect_8 = Rectangle((W-bW)//2, H - bH, bW, bH)
Rect_9 = Rectangle(W - bW, H - bH, bW, bH)
# RGB32 Format32bppRgb
# RGB24 Format24bppRgb
# RGB48 Format48bppRgb
# MONO8 Format8bppIndexed
# MONO16 Format16bppGrayScale
# RAW8 Format32bppRgb
# RAW16 Format64bppArgb
frame = 0
def framehandler(sender, args):
global frame
if (dumpdata):
try:
frame = args.Frame.GetFrameBitmap()
bitmap = Bitmap(3 * bW + 2, 3 * bH + 2)#, frame.PixelFormat)
gr = Graphics.FromImage(bitmap)
gr.DrawImage(frame, Rectangle( 0, 0, bW, bH), Rect_1, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( bW + 1, 0, bW, bH), Rect_2, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle(2*bW + 2, 0, bW, bH), Rect_3, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( 0, bH + 1, bW, bH), Rect_4, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( bW + 1, bH + 1, bW, bH), Rect_5, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle(2*bW + 2, bH + 1, bW, bH), Rect_6, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( 0, 2*bH + 2, bW, bH), Rect_7, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle( bW + 1, 2*bH + 2, bW, bH), Rect_8, GraphicsUnit.Pixel)
gr.DrawImage(frame, Rectangle(2*bW + 2, 2*bH + 2, bW, bH), Rect_9, GraphicsUnit.Pixel)
form.pictureBox1.Image = bitmap
except:
print("Problem framehandler")
# *****************************************************************************
def Nine_zone(self):
global dumpdata
global bH, bW, Rect_1, Rect_2, Rect_3, Rect_4, Rect_5, Rect_6, Rect_7, Rect_8, Rect_9
Boxsize = int(self.textBoxSize.Text)
bH = Boxsize
bW = Boxsize
Rect_1 = Rectangle(0, 0, bW, bH)
Rect_2 = Rectangle((W-bW)//2, 0, bW, bH)
Rect_3 = Rectangle(W - bW, 0, bW, bH)
Rect_4 = Rectangle(0, (H-bH)//2, bW, bH)
Rect_5 = Rectangle((W-bW)//2, (H-bH)//2, bW, bH)
Rect_6 = Rectangle(W - bW, (H-bH)//2, bW, bH)
Rect_7 = Rectangle(0, H - bH, bW, bH)
Rect_8 = Rectangle((W-bW)//2, H - bH, bW, bH)
Rect_9 = Rectangle(W - bW, H - bH, bW, bH)
self.pictureBox1.Size = System.Drawing.Size(3*Boxsize + 2, 3*Boxsize + 2)
if (3*Boxsize + 2 > 450):
new_width = int((3*Boxsize + 2)/1.476)
self.ClientSize = System.Drawing.Size(new_width, new_width + 40)
self.AutoScaleDimensions = SizeF(96, 96)
if (3*Boxsize + 2 < 450):
new_width = 300
self.ClientSize = System.Drawing.Size(new_width, new_width + 40)
self.AutoScaleDimensions = SizeF(96, 96)
if (self.Nine_zone.Text == "Start"):
self.Nine_zone.Text = "Stop"
self.Nine_zone.BackColor = Color.Red
dumpdata = True
SharpCap.SelectedCamera.FrameCaptured += framehandler
elif (self.Nine_zone.Text == "Stop"):
self.Nine_zone.Text = "Start"
self.Nine_zone.BackColor = Color.Gainsboro
dumpdata = False
SharpCap.SelectedCamera.FrameCaptured -= framehandler
print()
print("Nine Zones:")
print("*****************")
return()
# *****************************************************************************
class Nine_zoneWINMenuForm(Form):
def __init__(self):
self.SuspendLayout()
self.InitializeComponent()
self.setupCheckButtons()
self.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
self.AutoScaleDimensions = SizeF(96, 96)
self.ResumeLayout()
def InitializeComponent(self):
self.Text = "Nine Zones"
self.ClientSize = System.Drawing.Size(300, 340)
self.TopMost = True
def setupCheckButtons(self):
self.Nine_zone = Button()
self.Nine_zone.Text = "Start"
self.Nine_zone.Location = Point(20, 10)
self.Nine_zone.Click += self.Nine_zone_meas
self.Nine_zone.AutoSize = True
self.labelSize = Label()
self.labelSize.AutoSize = True
self.labelSize.Location = Point(110, 14)
self.labelSize.Name = "labelSize"
self.labelSize.Text = "Size:"
self.textBoxSize = TextBox()
self.textBoxSize.AutoSize = True
self.textBoxSize.Location = Point(145, 12)
self.textBoxSize.Name = "textBoxSize"
self.textBoxSize.Size = Size(35, 20)
self.textBoxSize.Text = str(Boxsize)
self.button_Exit = Button()
self.button_Exit.Text = "Exit"
self.button_Exit.Location = Point(200, 10)
self.button_Exit.Click += self.exit
self.button_Exit.AutoSize = True
self.pictureBox1 = System.Windows.Forms.PictureBox()
self.pictureBox1.Location = System.Drawing.Point(3, 40)
self.pictureBox1.Name = "pictureBox1"
self.pictureBox1.Size = System.Drawing.Size(3*Boxsize + 2, 3*Boxsize + 2)
self.pictureBox1.TabIndex = 0
self.pictureBox1.TabStop = False
self.Controls.Add(self.Nine_zone)
self.Controls.Add(self.labelSize)
self.Controls.Add(self.textBoxSize)
self.Controls.Add(self.button_Exit)
self.Controls.Add(self.pictureBox1)
def Nine_zone_meas(self, sender, event):
th = Thread(ParameterizedThreadStart(Nine_zone))
th.SetApartmentState(ApartmentState.STA)
th.Start(self)
def exit(self, sender, event):
print("Stop script")
dumpdata = False
SharpCap.SelectedCamera.FrameCaptured -= framehandler
self.Close()
form = Nine_zoneWINMenuForm()
form.StartPosition = FormStartPosition.CenterScreen
form.TopMost = True
form.Show()
You can change the size of the sub-zone. The script will show 9 sectors (4 corners + top, bottom, left, right and centre).
The scaling of the window may not work properly with a different monitor pixel scaling (windows 100%, 125% or 150%).
The 9 sectors are the image before the stretching.
Robin, is it possible to have the image after the "Display Histogram Stretch" ?