通过ChooseMenuItem()执行粒子分析-出了什么问题 [英] Performing Particle Analysis via ChooseMenuItem() - what is wrong

查看:84
本文介绍了通过ChooseMenuItem()执行粒子分析-出了什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题的继续.此处.
我正在重新发布答案之一,以使主题更加清晰.
原始问题(和后续问题)来自 user6406828 .

This is a continuation of the question here.
I am re-posting one of the answers to keep the topics cleaner.
The original question (and continued question) is from user6406828.

尝试执行一些(循环的)粒子分析,有时会抛出一些错误.此代码有什么改进之处?

Trying to perform some (looped over) particle analysis, a couple of errors are thrown every now and then. What can be improved in this code?


以下是一些代码:


Here are some code:

// $BACKGROUND$
number useBadImage, stopAtSrcImage // flags for test
image histogram, src, stack
taggroup imgLst
number i, imgCt,imgID,x0,y0,x1,y1, z1
if (OkCancelDialog("Generate a bad image?")) useBadImage=1
else useBadImage=0  
if (OkCancelDialog("Stop at testing image generation?")) stopAtSrcImage=1
else stopAtSrcImage=0  

x0=128;y0=128
x1=512;y1=512
image img:=exprSize(x0,y0,0)
z1=10
stack:=exprSize(x1,y1,z1)

img=random()*100
src:=exprSize(x1,y1,0)
if (useBadImage) {
    src=img.warp(icol*x0/x1,irow*y0/y1)
    for (i=0;i<z1;i++) {
        stack[0,0,i,x1,y1,i+1]=src+10
    }
}
else {
    src=sin(icol/pi())+cos(irow/pi())
    for (i=0;i<z1;i++) {
        stack[0,0,i,x1,y1,i+1]=src+0.1
    }
}
if (stopAtSrcImage) {
    src.showImage()
    exit(0)
}
void doThreshold(image histogram, number PctOffPeak, number AdditionalShift) {
    number max, lf,rt,maxX,maxY, i, val, threshold,d0,d1
    ROI r = NewROI(); // foreground ROI
    histogram.getSize(d0,d1)
    //histogram[0,0,1,5]=0 //remove zero peak
    max=histogram.max(maxX,maxY)
    threshold=max*PctOffPeak/100
    //okdialog("After trim zero peak, maxX=" +maxX)
    lf=0;rt=d0
    for (i=maxX;i<d0;i++) {
        val=histogram.getPixel(i,0)
        if (val<=threshold) {
            lf=i
            i=d0
        }
    }
    lf=lf+AdditionalShift
    r.ROISetRange(lf,rt);
    histogram.ImageGetImageDisplay(0).ImageDisplayAddRoi(r);
}
//main loop
for (i=0;i<z1;i++) {
    src:=stack[0,0,i,x1,y1,i+1].imageclone()
    src.showImage()
    ChooseMenuItem( "Analysis", "Particles", "Start Threshold" )
    histogram := GetFrontImage();
    doThreshold(histogram, 20,2)
    if( !_FloatingModelessDialog( "Adjust threshold level","Proceed!" ) ) {; histogram.deleteimage(); exit(0); };
    else {
        src.showImage()
        delay(60)
        ChooseMenuItem( "Analysis", "Particles", "Remove Edge Particles" )
        delay(60)
        ChooseMenuItem( "Analysis", "Particles", "Find Particles" )
        delay(60)
        ChooseMenuItem( "Analysis", "Particles", "Analyze Particles" )
        delay(60)
        histogram.deleteImage()
    }
}

如果选择"useBadImage"和"stopAtSrcImage",则显示第一个src图像.对于粒子分析而言,这将是非常糟糕的. UI操作将生成多个无效索引"错误(尝试选择直方图中强度最高的15%).如果useBadImage == 0,则图像表现更好.

If select "useBadImage" and "stopAtSrcImage", the first src image is shown. That would be a very bad one for particle analysis. The UI operation will generate multiple "invalid index" error (try select top 15% intensity in histogram). If useBadImage==0, then the image behaves better.

我扩展代码以在图像堆栈中运行一些循环.对于具有好"图像的图像堆栈,手动粒子分析可以顺利通过所有图层,而不会出现问题,但是循环几乎总是在循环中的某个位置生成异常"(在结果"窗口中显示).似乎增加长时间的延迟无济于事.但是毫不拖延地肯定会导致循环崩溃. doThreshold(image histogram, number PctOffPeak, number AdditionalShift)假定为找到直方图的最大值,从此处开始,右移峰的某个百分比,添加AdditionalShift,然后在此处为ROI范围设置"lf"值.但这并不总是表现为预期的.

I extend the code to run some loop through an image stack. For an image stack with "good" images, the manual particle analysis can go through well with all the layers without problem, but the loop almost always generate "Exceptions" (shown in Result window) somewhere in the loop. Seems adding long delay does not help. But without delay definitely crashes the loop. The doThreshold(image histogram, number PctOffPeak, number AdditionalShift) was suppose to "Find the max in histogram, start from there, go right to some percentage off the peak, add the AdditionalShift, and set the "lf" value for ROI range there. But this does not always behave as expected.

推荐答案

在尝试处理隐藏的错误时,我制定了自己的粒子分析版本.速度不是大问题.这是一些核心功能,

While trying to deal with the hidden error, I worked out my own version of particle analysis. The speed is not a big issue. Here are some core functions,

image dxImg,dyImg
void getSearchImg() {
    dxImg:=[8,1]:
    {
        {-1, 0, 1, 1, 1, 0,-1,-1}
    }
    dyImg:=[8,1]:
    {
        {-1,-1,-1, 0, 1, 1, 1, 0}
    }
}
number pop(taggroup &tg, number &x, number &y) {
    number n, mode
    n=tg.TagGroupCountTags()
    if (n==0) return -1
    else { //stack, last in, first out
        tg.TagGroupGetIndexedTagAsLongPoint(n-1,x,y)
        tg.TagGroupDeleteTagWithIndex(n-1)
    }
    return 1
}
void push(taggroup &tg, number x, number y) {
    tg.TagGroupInsertTagAsLongPoint(infinity(),x,y)  
}
taggroup dfs(image img) {//Depth-first search, Using stack
    number isOn, i,xInc, yInc,d0,d1
    number status, x0, y0,x1,y1, x, y, val
    taggroup tgStack=newtaglist()
    taggroup tgClusters=newtaglist()
    taggroup tgSingleCluster=newTaglist() //this is global
    roi r
    image imgTemp:=img.ImageClone()
    imgTemp.getSize(d0,d1)
    getSearchImg()
    imgTemp=tert(icol==0||irow==0||icol==d0-1||irow==d1-1, 0, imgTemp)
    while (1) {
        status=1
        val = imgTemp.max(x, y)
        if (val<=0) break
        else {
            tgSingleCluster.TagGroupInsertTagAsLongPoint(infinity(),x,y)
            tgStack.push(x,y)
            imgTemp[x,y]=0
            x0=x;y0=y
            while (status==1) {
                for (i=0;i<8;i++) {
                    xInc=dxImg.getPixel(i,0)
                    yInc=dyImg.getPixel(i,0)
                    x=x0+xInc;y=y0+yInc
                    isOn=imgTemp.getPixel(x,y)
                    if (isOn) {
                        imgTemp[x,y]=0
                        tgSingleCluster.TagGroupInsertTagAsLongPoint(infinity(),x,y)
                        tgStack.push(x,y)
                    }
                }
                status=tgStack.pop(x0,y0)
            }
            tgClusters.TagGroupInsertTagAsTagGroup(infinity(), tgSingleCluster) 
            tgSingleCluster=newTaglist()
            tgStack=newtaglist()
        }
    }
    return tgClusters
}

这篇关于通过ChooseMenuItem()执行粒子分析-出了什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆