2019年8月4日

用Imagej製作灰階影像加彩色線條的視覺效果


前幾天看到網路上流傳了一個在灰階影像上加上彩色線條之後,看起來就好像是彩色圖片的視覺效果,我看了覺得很有趣,很想自己來動手玩一玩。於是就用imagej玩看看,順便寫了一個code script放在文章底下,有興趣的可以複製貼上玩玩看。

可以做什麼效果呢?可以針對個別區域製作橫紋效果。下面組圖是用同樣粗細的線寬,但是不同的線距


也可以做出斜紋效果



或是直紋效果



這些線寬都不一樣,但是線寬和線距比例都是1:3



同樣的code改一改,也可以做出網紋的效果



以下就是教學影片了,前兩部是從原理製作開始講,如果要直接使用code來做的人,就從第三部來看就可以了。





以下就是imagej的macro囉
===========================
lineW = 2;
space = 8;
angle = 0;


run("Select None");
rename("color");
run("Duplicate...", "title=grey");

selectWindow("grey");
run("8-bit");
run("RGB Color");
run("Set Measurements...", "area bounding redirect=None decimal=3");
roiManager("Select", 0);
run("Rotate...", "angle="+ angle +"");
roiManager("Add");
run("To Bounding Box");
run("Measure");

BX = getResult("BX", nResults-1);
BY = getResult("BY", nResults-1);
Width = getResult("Width", nResults-1);
Height = getResult("Height", nResults-1);

lineN = floor(Height/(lineW+space));
print(lineN);
for (i = 0; i < lineN; i++) {
//建立選取區
run("Specify...", "width="+ Width +" height="+ lineW +" x="+ BX +" y="+ BY+(space+lineW)*i +"");
//run("Rotate...", "angle="+ angle +"");
roiManager("Add");
//交集運算
roiManager("Select", newArray(1,roiManager("count")-1));
roiManager("AND");
//產生要繪圖的選取區ROI
roiManager("Add");
roiManager("deselect")
roiManager("Select", roiManager("count")-2);
roiManager("Delete");

//roiManager("Select", roiManager("count")-1);
//roiManager("Delete");
}

a1 = newArray(roiManager("count")-2);
for (i=0; i<a1.length; i++)
  a1[i] = i+2;
roiManager("Select", a1);
roiManager("Combine");
roiManager("Add");

a1 = newArray(roiManager("count")-2);
for (i=0; i<a1.length; i++)
  a1[i] = i+1;
roiManager("Select", a1);
roiManager("Delete");


roiManager("Select",roiManager("count")-1);
run("Rotate...", "angle="+ -angle +"");
roiManager("Add");

//====複製原圖,貼到黑白圖====
selectWindow("color");
roiManager("Select", roiManager("count")-1);
run("Copy");

selectWindow("grey");
roiManager("Select", roiManager("count")-1);
run("Paste");
//=========================

roiManager("Select", newArray(roiManager("count")-1,roiManager("count")-2));
roiManager("Delete");

selectWindow("color");
run("Select None");

selectWindow("grey");
run("Select None");
rename(lineW+":"+space);

text = "w"+lineW+":space"+space;
setFont("SansSerif", 14, " antialiased");
makeText(text, 20, getHeight()-20);
run("Add Selection...", "stroke=yellow fill=#660000ff new");
run("Select None");