Skip to content

How to convert ROI coordinates from relative to pixels #232

@Kees-van-der-Oord-Nikon

Description

  • nd2 version: 5.42.06
  • Python version: 3.9
  • Operating System: W10

Description

This is not really an issue, just a suggestion to add to the documentation

What I Did

The code below converts the ROI coordinates from relative fractions to pixel coordinates.
The Ellipse ROI width and height are before rotation over the indicated angle.
The only missing detail is the inner circle of the Ring ROI.

f = nd2.ND2File(sys.argv[1])

mid_x = f.shape[1] / 2.
mid_y = f.shape[0] / 2.

def RelToPixSizeX(x):
	return mid_x * x

def RelToPixSizeY(y):
	return mid_y * y

def RelToPixPosX(x):
	return mid_x + mid_x * x

def RelToPixPosY(y):
	return mid_y + mid_y * y

for id in f.rois:
	roi = f.rois[id]
	header = str(roi.id) + ";" + nd2.structures.RoiShapeType(roi.info.shapeType).name + ";"
	pnts = ""
	info = roi.animParams[0]
	if (roi.info.shapeType == nd2.structures.RoiShapeType.Rectangle) or \
			(roi.info.shapeType == nd2.structures.RoiShapeType.Square) or \
			(roi.info.shapeType == nd2.structures.RoiShapeType.Circle) or \
			(roi.info.shapeType == nd2.structures.RoiShapeType.Ellipse) or \
			(roi.info.shapeType == nd2.structures.RoiShapeType.Ring):
		pnts = "(" + \
			"{:#.1f}".format(RelToPixPosX(info.centerX)) + "," + \
			"{:#.1f}".format(RelToPixPosY(info.centerY)) + "),(" + \
			"{:#.1f}".format(RelToPixSizeX(info.boxShape.sizeX)) + "," + \
			"{:#.1f}".format(RelToPixSizeY(info.boxShape.sizeY)) + ")"
	elif (roi.info.shapeType == nd2.structures.RoiShapeType.Line) or \
			(roi.info.shapeType == nd2.structures.RoiShapeType.Polygon) or \
			(roi.info.shapeType == nd2.structures.RoiShapeType.PolyLine) or \
			(roi.info.shapeType == nd2.structures.RoiShapeType.Bezier):
		for pnt in roi.animParams[0].extrudedShape.basePoints:
			if len(pnts) != 0:
				pnts += ","
			pnts += "(" + "{:#.1f}".format(RelToPixPosX(pnt.x+info.centerX)) + "," + "{:#.1f}".format(RelToPixPosY(pnt.y+info.centerY)) + ")"
	print(header + pnts + ";" + "{:#.4f}".format(info.rotationZ))

f.close()

WR,

Kees

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions