横向きスキャンした場合の、指定したフォルダ内の全ての画像の「横線ノイズ」を検出し、補正するためのマクロです。
ベースはこちらの縦線ノイズ除去マクロ(高速版)です。
//Horizontal_Noise_Correction_HiSp.txt
//setBatchMode(true);
//Folder setting
showMessage("Select Open Folder");
openDir = getDirectory("Choose a Directory");
showMessage("Select Save Folder");
saveDir = getDirectory("Choose a Directory");
list = getFileList(openDir);
//JPEG quality
quality=90;
run("Input/Output...", "jpeg=quality gif=-1 file=.csv save_column save_row");
//Segmentation setting
xSeg=20;
ySeg=10;
//BlackZone threshold(L_mean)
TH_kuroBeta=30;
//WhiteLine threshold(s_mean)
TH_whiteLine=20;
//HighIntensity threshold
TH_HighIntensity=20;
//HighCount threshold
TH_countRatio=80;
//operation
x=0;
y=0;
detectedCount=0;
rejectCounter=0;
for (i=0; i<list.length;i++){
open(openDir+list[i]);
run("Rotate 90 Degrees Right");
//progress
print(i+1,"/",list.length,"...Progress=",floor((i+1)/list.length*10000)/100,"%");
saveFlag=0;
correctCount=0;
sum=0;
width=getWidth();
height=getHeight();
//value of movement
xStep=floor(xSeg/2);
yStep=floor(ySeg/2);
depth=bitDepth();
if (depth==8) {
//Segmentation section
segCount=1;
for(y=0;y<height-ySeg;y=y+yStep){
for(x=0;x<width-xSeg;x=x+xStep){
for(xi=0;xi<xSeg;xi++){
for(yi=0;yi<ySeg;yi++){
pixelValue=getPixel(x+xi,y+yi);
sum=sum+pixelValue;
}
}
L_mean=sum/xSeg/ySeg;
sum=0;
print(i+1,"/",list.length,"...Progress=",floor((i+1)/list.length*10000)/100,"%",", ","Segment No=",segCount);
segCount++;
//recognize kuroBeta
if(L_mean<TH_kuroBeta){
a=x+5;
b=y;
//small rectangle ROI scan
for(ii=1;ii<xSeg-9;ii++){
b=y;
for(yi=0;yi<ySeg;yi++){
pixelValue=getPixel(a,b+yi);
sum=sum+pixelValue;
}
s_mean=sum/ySeg;
sum=0;
//horizontal direction scan on detected whiteLine
if(s_mean>TH_whiteLine){
count=0;
for(jj=1;jj<=ySeg;jj++){
pixel_intensity=getPixel(a,b);
//count HighIntensity
if(pixel_intensity>TH_HighIntensity){
count++;
b++;
}
}
//Noise Correction
if(count>ySeg*TH_countRatio/100){
correctCount++;
print("(",a,",",b,")","Noise Correction!!",correctCount);
saveFlag=1;
b=y;
for(iii=1;iii<=ySeg;iii++){
beta=random();
beta=L_mean-beta*10;
if(beta<0)beta=0;
correctedValue=floor(beta);
setPixel(a,b,correctedValue);
b=b+1;
}
}
}
a=a+1;
}
}
}
}
}else{
rejectCounter++;
print("...Color Image!");
}
if(saveFlag==1){
run("Rotate 90 Degrees Left");
name=getTitle();
dotIndex=lastIndexOf(name,".");
title=substring(name,0,dotIndex);
newname = title+".jpg";
//newname = title+".png";
rename(newname);
saveAs("Jpeg", saveDir+newname);
//saveAs("PNG", saveDir+newname);
detectedCount++;
}
close();
}
print("DetectedCounts=",detectedCount);
print("oshimai");
画像引用:弐瓶, 人形の国, コミックス第2巻, 76ページ, 講談社, 2018
ポイント
細かな説明は縦線ノイズ除去マクロ(高速版)を参照してもらいたいので、ここでは上記コードのポイントだけかいつまんで。
イ. 横線を検出するため、画像を90°回転させる命令を入れました。検出・補正後、元に戻してから保存してくれます。
ロ. 黒ベタが少し粒状感(ざらつき)が残る程度でも綺麗に仕上がるように、correctedValueにランダム要素を乗せました。黒ベタが0近辺の場合にも対応してるので、これ一本で十分OK。
ハ. パラメータ設定により大きく仕上がりが変わるので、必ず自分でいじってテストしてから!
マクロの起動方法
①ImageJ上部タブの[Plugins]→[New]→[Macro]で起動したエディタに、記事の一番上のコードをコピペしてtxtファイル(Horizontal_Noise_Correction_HiSp.txt)を作成・保存する。
②保存したファイルをImageJフォルダ内の[plugins]フォルダにしまう。
このとき、[plugins]フォルダの中に新たに適当な名前のフォルダを作って、その中にしまってもOKです。ここでは仮に「自炊」というフォルダにtxtファイルを突っ込んだとします。
③一度ImageJを再起動すると、マクロがインストールされ、起動準備OK。
④上部タブ[Plugins]→[自炊]→[Horizontal Noise Correction HiSp]でマクロが実行されます。
ライセンスなんかは一切無いので、ぜひぜひ自由に使ってみてください!
2020/03/08 追記
そもそも縦線の発生しづらいデータを作るためのコツです。実は縦線対策にはこのマクロが一番オススメです!